61

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.

1
  • i'm using Angular v20 and here you can also set the "apptInterval: ReturnType<typeof setInterval>". this should be a pure typescript definition of the return value of setInterval Commented Jun 3 at 12:43

5 Answers 5

147

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"
  ]
}
Sign up to request clarification or add additional context in comments.

7 Comments

@LuigiRubino Glad i could help!
It also worked for me! Note: it also solved the previous webpack compile error: Cannot find name 'require'. (error ts2304)
You can also add types: ["node"] to tsconfig.json, but make sure you don't have an empty types: [] definition in tsconfig.app.json because that overrides it (learned that the hard way...)
Could you explain why this is the fix?
@BrentonScott - can you elaborate, what is the question? it is the fix because it fixes the original issue - error: TS2503: Cannot find namespace 'NodeJS'.
|
20

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

4 Comments

Unfortunately, neither option made any difference.
Open your code in VSCode, and see if NodeJS exists in the intelliSense. It should be there is you have things setup correclty.
Thanks! I forgot to include "**/*.d.ts" in my tsconfig.app.json within the include array. Now my custom type definitions work again.
This did the trick for me!
8

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

Comments

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.