我定义了以下Angular2组件:

import {Component} from 'angular2/core';

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

当我试图编译它时,第5行出现以下错误:

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

我相信模块.id指的是CommonJS module变量(参见here).我在tsconfig中指定了CommonJS模块系统.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
}

如何更正打字脚本错误?

推荐答案

Update

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

npm i @types/node --save-dev

(不用--save-dev,只需使用快捷方式-D即可)

或者在全局安装:

npm i @types/node --global

您还可以在tsconfig中指定typeRootstypes.json,如果你想要的话.

Old version

您需要安装 node 环境依赖项.打字.json

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

另一种方法是使用typings manager全局安装 node 定义文件:

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

然后是你的打字.json文件如下所示:

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

Typescript相关问答推荐

类型保护,用于验证是否定义了二元组的每个项

发出与HTML多姆事件重叠的Angular事件

Typescript如何从字符串中提取多个文本

为什么缩小封闭内部不适用于属性对象,但适用于声明的变量?

TypeScript泛型参数抛出错误—?&#"' 39;预期"

如何在函数参数中使用(或模仿)`success`的行为?

Angular中的其他服务仅获取默认值

接口的额外属性作为同一接口的方法的参数类型'

从typescript中的对象数组中获取对象的值作为类型'

使用axios在ngOnInit中初始化列表

无法从Chrome清除还原的初始状态数据

使用2个泛型类型参数透明地处理函数中的联合类型

用于验证字符串变量的接口成员

在Reaction-Native-ReAnimated中不存在Animated.Value

剧作家元素发现,但继续显示为隐藏

如何使用IsEqual创建断言类型相等的实用程序

Vite+Chrome扩展 list v3-;不能在模块外使用import语句;对于inpage脚本

React Context TypeScript错误:属性';firstName';在类型';iCards|iSearch';

如何键入';v型';

Karma Angular Unit测试如何创建未解析的promise并在单个测试中解决它,以判断当时的代码是否只在解决后运行