angulartypescriptwebpack

Cannot find namespace NodeJS after webpack upgrade


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.


Solution

  • 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"
      ]
    }