I have Angular2 application that is built with WebPack. I upgraded WebPack from v1.8 to v2, and everything seems to be working fine. The only problem is that the old code has the following:
import Timer = NodeJS.Timer;
....
apptInterval: Timer;
....
this.apptInterval = setInterval(() => { ... }, 1000);After the upgrade this gives me an error: TS2503: Cannot find namespace 'NodeJS'.
tsconfig.json looks like this:
{
"compilerOptions": { "target": "es5", "module": "commonjs", "moduleResolution": "node", "sourceMap": true, "emitDecoratorMetadata": true, "experimentalDecorators": true, "lib": ["es2015", "dom"], "noImplicitAny": true, "suppressImplicitAnyIndexErrors": true }
}The docs for Angular/Webpack no longer have typings.json; however, even if I copy from Webpack 1 directory, it doesn't help. The content is typings.json is
{
"globalDependencies": { "jasmine": "registry:dt/jasmine#2.2.0+20160621224255", "node": "registry:dt/node" }
}Interesting that if I remove references to NodeJS, everything works fine. Like this:
apptInterval: any;
....
this.apptInterval = setInterval(() => { ... }, 1000);If I look under F12 debugger, the type of apptInterval is ZoneTask. I am sure there is a way to make it strong-typed, but it escapes me. The only suggestion I found was to run typings install dt~node --global --save-dev (which essentially updates typings.json, but doesn't help.
4 Answers
If you also have tsconfig.app.json in your working directory, try to add the attribute node to the types field. Like:
{ "extends": "../tsconfig.json", "compilerOptions": { "outDir": "../out-tsc/app", "module": "es6", "baseUrl": "", "types": ["node"] --> ADD THIS }, "exclude": [ "test.ts", "**/*.spec.ts" ]
} 3 For [email protected]+, use @types:
npm install -D @types/node @types/jasmineIf you still want to hang on to typings, include typings/index.d.ts to your tsconfig.json:
{ "include": [ "typings" ], // or
"files": [ "typings/index.d.ts" ]
} 4 Adding node to the types didn't work for me, "types": ["node"] but adding to the type roots did
{ "compilerOptions": { ... "typeRoots": [ "node_modules/@types" ] }
} I'm running Angular 12 and I have strict mode enabled, I had to add both Fzum and johnny S's answers to my tsconfig.app.json. The only time it didn't throw an error in console is when I had both.
My tsconfig.app.json looks like this:
{ "compilerOptions": { ... "types": ["node"], "typeRoots": [ "node_modules/@types" ] }
}Sorry for making this a separate answer, I don't have enough reputation to comment yet