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?
module.id
cannot find the name module.
Follow following steps to resolves this issue,
step 1: Install node module by using the below command
Step 2: modify the file
tsconfig.app.json
undersrc/app
For those looking to do this with Typescript 2.x and @types, here's what you need to do:
npm install -D @types/node
types: ["node"]
undercompilerOptions
in yourtsconfig.json
file.I had a tough time finding the instructions for step 2.
Reference: Typescript issue 9184
Edit:
You could also do:
"typeRoots": [ "node_modules/@types" ]
undercompilerOptions
in yourtsconfig.json
file. This is instead of thetypes
property and has the benefit of automatically including any@types
you install with npm.For example:
[Second edit] Apparently, with the latest typescript, you only need
typeRoots
ortypes
iftsconfig.json
is not in the root of your project or your types are not stored innode_modules/@types
.I hit this error when porting my @angular/angular2 Quickstart project into a new angular cli auto-generated project.
It seems that
moduleId: module.id
isn't needed anymore.This is the latest auto-generated component:
Removing all occurances resolved my errors.
I installed in the project, and worked well
Instead of "ambient" try "global" by Typings 1.0
Update
If you use Typescript 2^ just use the following command:
(instead of
--save-dev
you can just use shortcut-D
)or install it globally:
You can also specify
typeRoots
ortypes
in your tsconfig.json if you want but by default all visible “@types” packages are included in your compilation.Old version
You need to install node ambientDependencies. Typings.json
Another way can use typings manager to install node definition file globally:
Then your typings.json file will look like this: