繁体   English   中英

required 关键字不适用于 angular

[英]required keyword not working with angular

require关键字不适用于 angular,引发错误:

src/app/app.component.ts(8,16): 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.

我该如何解决? 其实我也用nodejs正确安装了它,但没有工作。

  1. 正如其他人所说,您需要require吗? 使用 ES6 导入。
  2. 但是,如果您确实需要require ,则可以在它之前加上declare const require: any 为我工作。

如果您想在代码中使用 require ,请按照以下步骤操作

首先安装这个包

npm i -D @types/node

然后在你的 tsconfig.json

"typeRoots": [
            "node_modules/@types", //add this
        ],

而不是使用:

import { Process } from '@types/node'; 您需要更改tsconfig.json:

{
"compilerOptions": {
...
"typeRoots": ["node_modules/@types"]
}
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM