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 {
}
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'.
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
}
How can I correct the TypeScript error?
Two key points:
Register typings by running
typings install dt~node --global --save
. So you'll get the following section intypings.json
:Add reference to the new module. Two ways:
Directly add a reference to a dependency in your TS
/// <reference path="../../../typings/globals/node/index.d.ts" />
Add
typings/index.d.ts
in thefiles
section of thetsconfig.json
See more here.
This is what I did that worked for me on Eclipse(Webclipse)/Windows.
Step 1:
Terminal
Step 2:
tsconfig.json
In addition, I had the following dependencies in my package.json, so I was using typescript 2.
I use VS 2015, and had same issues, but I have resolved using:
add the typings.json file from the angular.io website (2.0.0 final at the moment) and the run:
then
in the package.json I have
in the typings.json I have the following configuration
I had to add the types as an empty array
p.s. to me personally is a strange issue, because as soon as you exclude typings inside typings.json, you have immediately highlighted 'module', but if you let it in, you have lot's of duplicates. Don't know who to blame, me, typescript or visual studio :)