我已经定义了以下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'.
我相信module.id是指CommonJS模块变量(见here)。我在tsconfig.json中指定了CommonJS模块系统:
{ "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错误?
您需要安装节点ambientDependencies。 Typings.json
"ambientDependencies": { "node": "github:DefinitelyTyped/DefinitelyTyped/node/node.d.ts#138ad74b9e8e6c08af7633964962835add4c91e2",
另一种方式可以使用typings管理器全局安装节点定义文件:
typings install dt~node --global --save
然后你的typings.json文件将如下所示:
"globalDependencies": { "node": "registry:dt/node#6.0.0+20160608110640" }
如果使用Typescript 2 ^只需使用以下命令:
npm install @types/node --global --save
或者在项目中安装:
npm install @types/node --save
然后从引导中添加对它的引用:
/// <reference path="../../node_modules/@types/node/index.d.ts" />