Angular 使用 require 出现的问题: Cannot find name process Do you need to install ...

最近更新时间 2020-11-20 12:48:40

在 Angular 项目中使用了 require 关键字 ,报错如下:

src/utils/api.ts:4:15 - error TS2591: Cannot find name 'require'. Do you need to install type definitions for node? Try `npm i @types/node` and then add `node` to the types field in your tsconfig.
4 const axios = require('axios');
#axios
const axios = require('axios');

解决办法:

修改 tsconfig.app.json 文件增加 “node” 参数,如下所示:

/* To learn more about this file see: https://angular.io/config/tsconfig. */
{
  "extends": "./tsconfig.json",
  "compilerOptions": {
    "outDir": "./out-tsc/app",
    "types": [
      "node"
    ]
  },
  "files": [
    "src/main.ts",
    "src/polyfills.ts"
  ],
  "include": [
    "src/**/*.d.ts"
  ]
}
可能喜欢
最近浏览
正在加载浏览记录