cannot find name 'require' in angular 7(ty

2020-02-10 03:01发布

My question is why this error shown?

ERROR in src/app/p2p/p2p.component.ts(14,16): error TS2580: Cannot find name 'require'. Do you need to install type definitions for node? Try npm i @types/node.

I have install

 @types/node
in app/tsconfig.app.json have add 
"types": [
    "node" 
  ],
  "typeRoots": [ 
  "../node_modules/@types"
 ]  

but there is error cannot find 'require'

6条回答
放荡不羁爱自由
2楼-- · 2020-02-10 03:06

check tsconfig.json too. you need to add the same settings there too.

查看更多
太酷不给撩
3楼-- · 2020-02-10 03:15

Like some other folks, I, too, had added node to the 'types' array in tsconfig and for some reason it made no difference. Knowing full well that this is a hack, here's how I resolved it:

Add this line anywhere above the 'require' statement: declare const require: any;

This is not a real fix, but I don't have the time to battle with this type of plumbing problem right now. I'll come back and deal with it later (or probably not, but that's OK, too)

查看更多
干净又极端
4楼-- · 2020-02-10 03:17

Type node is missing

install @types/node :

npm install --save @types/node

or

yarn add @types/node

edit your src/tsconfig.json adding:

{
    "compilerOptions": {
        "types": ["node"]
    }
}
查看更多
ゆ 、 Hurt°
5楼-- · 2020-02-10 03:19

Add the following settings to src/tsconfig.app.json

{
  "compilerOptions": {
    "types": ["node"]
  }
}
查看更多
Emotional °昔
6楼-- · 2020-02-10 03:25

The problem also remained after adding it to my tsconfig.json, but additionaly adding the following line to tsconfig.app.json resolved it for me:

{
"compilerOptions": {
    "types": ["node"]
}

So be sure to add this into both files ./tsconfig.json AND ./src/tsconfig.app.json and it should work.

查看更多
小情绪 Triste *
7楼-- · 2020-02-10 03:25

Its all because typeScript is not aware of the require keyword so do add a line making typescript aware of require keyword

once its compiled to javascript it knows the require word better and make the work done

declare var require: any;

const pokemon = require('src/assets/pokedex.json');
查看更多
登录 后发表回答