简体   繁体   English

required 关键字不适用于 angular

[英]required keyword not working with angular

require keyword is not working with angular, throwing an error: 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.

How can I resolve it?我该如何解决? Actually I have installed it with nodejs properly also, but not working.其实我也用nodejs正确安装了它,但没有工作。

  1. As others said, do you need require ?正如其他人所说,您需要require吗? Go with ES6 imports.使用 ES6 导入。
  2. If, however, you do need require , you can precede it with declare const require: any .但是,如果您确实需要require ,则可以在它之前加上declare const require: any Worked for me.为我工作。

If you want to use require in your code follow these step如果您想在代码中使用 require ,请按照以下步骤操作

First instal this pacakge首先安装这个包

npm i -D @types/node

Then in your tsconfig.json然后在你的 tsconfig.json

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

Instead of using: 而不是使用:

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

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

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

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