Felix
Felix

Reputation: 10078

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.

Upvotes: 57

Views: 62922

Answers (5)

Luther
Luther

Reputation: 280

For me, I had to simply update the zone.js package version to ^0.11.1 and the error disappeared.

Upvotes: 0

unional
unional

Reputation: 15589

For [email protected]+, use @types:

npm install -D @types/node @types/jasmine

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

Upvotes: 19

Deon du Preez
Deon du Preez

Reputation: 81

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

Upvotes: 8

johnny 5
johnny 5

Reputation: 20995

Adding node to the types didn't work for me, "types": ["node"] but adding to the type roots did

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

Upvotes: 4

Fzum
Fzum

Reputation: 2055

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

Upvotes: 139

Related Questions