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"
  ]
}
rss_feed