typescript webpack升级后找不到命名空间NodeJS

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42940954/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 04:22:15  来源:igfitidea点击:

Cannot find namespace NodeJS after webpack upgrade

angulartypescriptwebpack

提问by Felix

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:

我有使用 WebPack 构建的 Angular2 应用程序。我将 WebPack 从 v1.8 升级到 v2,一切似乎都运行良好。唯一的问题是旧代码有以下几点:

import Timer = NodeJS.Timer;
....
apptInterval: Timer;
....
this.apptInterval = setInterval(() => { ... }, 1000);

After the upgrade this gives me an error: TS2503: Cannot find namespace 'NodeJS'.

升级后,这给了我一个错误: TS2503: Cannot find namespace 'NodeJS'.

tsconfig.json looks like this:

tsconfig.json 看起来像这样:

{
"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

Angular/Webpack 的文档不再有typings.json; 但是,即使我从 Webpack 1 目录复制,也无济于事。内容是typings.json是

{
"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:

有趣的是,如果我删除对 NodeJS 的引用,一切正常。像这样:

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.

如果我查看 F12 调试器,apptInterval 的类型是ZoneTask. 我确信有一种方法可以使它成为强类型,但它让我无法理解。我发现的唯一建议是运行typings install dt~node --global --save-dev(它基本上更新了typings.json,但没有帮助。

回答by Fzum

If you also have tsconfig.app.jsonin your working directory, try to add the attribute nodeto the typesfield. Like:

如果您的工作目录中还有tsconfig.app.json,请尝试将属性节点添加到types字段。喜欢:

  {
  "extends": "../tsconfig.json",
  "compilerOptions": {
    "outDir": "../out-tsc/app",
    "module": "es6",
    "baseUrl": "",
    "types": ["node"] --> ADD THIS
  },
  "exclude": [
    "test.ts",
    "**/*.spec.ts"
  ]
}

回答by unional

For [email protected]+, use @types:

对于 [email protected]+,使用@types

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

If you still want to hang on to typings, include typings/index.d.tsto your tsconfig.json:

如果您仍然想继续使用typingstypings/index.d.ts请将其包含到您的 tsconfig.json 中:

{
  "include": [   // or "files"
    "typings/index.d.ts"
  ]
}

回答by johnny 5

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

将节点添加到类型对我不起作用,"types": ["node"]但添加到类型根对我 有用

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