cannot find name 'require' in angular 7(typescript 3.1.3)
Asked Answered
M

6

33

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'

Madox answered 2/11, 2018 at 9:14 Comment(2)
show your p2p.component.tsTightlipped
declare var require : any; export class P2pComponent implements OnInit { constructor() { var Peer = require(peer) var p = new Peer({ initiator: location.hash === '#1', trickle: false }) p.on('signal', function (data) { console.log('SIGNAL', JSON.stringify(data)) document.querySelector('#outgoing').textContent = JSON.stringify(data) }) document.querySelector('form').addEventListener('submit', function (ev) { ev.preventDefault()}) p.on('connect', function () { console.log('CONNECT') p.send('whatever' + Math.random()) }) } }Madox
U
45

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.

Uninterested answered 4/4, 2019 at 9:45 Comment(5)
For me worked when I added it to tsconfig.app.json! Thanks a lot!Trapeze
I found I could add "node" to "types" in tsconfig.app.json without also including it in tsconfig.json and the project compiled for me.Butterflies
Winner! The problem is that a default tsconfig.app.json has an empty types property, which obliterates the one from tsconfig.json. Took me a while to work that outEnwrap
This works for react or any typescript projectAtrocity
@DaveNottage input helped me, removing empty types from tsconfig.app.json and added types=["node"] in tsconfig.json fixed the issue for me.Pomcroy
E
31

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

{
  "compilerOptions": {
    "types": ["node"]
  }
}
Eldredge answered 18/1, 2019 at 15:25 Comment(0)
A
21

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"]
    }
}
Alvie answered 2/11, 2018 at 9:18 Comment(0)
H
5

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)

Hives answered 2/2, 2019 at 18:18 Comment(2)
Did you get a proper solution mate?Wage
No, never came back to revisit thisHives
K
1

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

Kinghorn answered 4/1, 2019 at 15:48 Comment(0)
T
1

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');
Taproom answered 18/6, 2019 at 10:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.