本文介绍了Angular2代码中的TypeScript错误:找不到名称“模块"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我定义了以下Angular2组件:

I have defined the following Angular2 component:

import {Component} from 'angular2/core';

@Component({
  selector: 'my-app',
  moduleId: module.id,
  templateUrl: './app.component.html'
})
export class AppComponent {
}

当我尝试对此进行编译时,在第5行出现以下错误:

When I try to compile this, I get the following error on line 5:

src/app/app.component.ts(5,13): error TS2304: Cannot find name 'module'.

我相信module.id指的是CommonJS module变量(请参见此处).我在tsconfig.json中指定了CommonJS模块系统:

I believe module.id is referring to the CommonJS module variable (see here). I have specified the CommonJS module system in tsconfig.json:

{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "declaration": false,
    "removeComments": true,
    "noLib": false,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "sourceMap": true,
    "pretty": true,
    "allowUnreachableCode": false,
    "allowUnusedLabels": false,
    "noImplicitAny": true,
    "noImplicitReturns": true,
    "noImplicitUseStrict": false,
    "noFallthroughCasesInSwitch": true
  },
  "exclude": [
    "node_modules",
    "dist",
    "typings/browser.d.ts",
    "typings/browser",
    "src"
  ],
  "compileOnSave": false
}

如何纠正TypeScript错误?

How can I correct the TypeScript error?

推荐答案

更新

如果使用 Typescript 2 ^ ,只需使用以下命令:

If you use Typescript 2^ just use the following command:

npm i @types/node --save-dev

(代替--save-dev,您只能使用快捷键-D)

(instead of --save-dev you can just use shortcut -D)

或在全局安装:

npm i @types/node --global

如果需要,还可以在tsconfig.json中指定typeRootstypes,但 默认情况下,所有可见的"@types"包均包含在您的编译文件中 .

You can also specify typeRoots or types in your tsconfig.json if you want but by default all visible "@types" packages are included in your compilation.

旧版本

您需要安装节点EnvironmentalDependencies. Typings.json

You need to install node ambientDependencies. Typings.json

"ambientDependencies": {
    "node": "github:DefinitelyTyped/DefinitelyTyped/node/node.d.ts#138ad74b9e8e6c08af7633964962835add4c91e2",

另一种方法可以使用类型管理器来全局安装节点定义文件:

Another way can use typings manager to install node definition file globally:

typings install dt~node --global --save-dev

然后您的inputs.json文件将如下所示:

Then your typings.json file will look like this:

"globalDependencies": {
  "node": "registry:dt/node#6.0.0+20160608110640"
}

这篇关于Angular2代码中的TypeScript错误:找不到名称“模块"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 05:22