简体   繁体   中英

required keyword not working with angular

require keyword is not working with angular, throwing an error:

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.

  1. As others said, do you need require ? Go with ES6 imports.
  2. If, however, you do need require , you can precede it with declare const require: any . Worked for me.

If you want to use require in your code follow these step

First instal this pacakge

npm i -D @types/node

Then in your tsconfig.json

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

Instead of using:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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