Reputation: 25763
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?
Upvotes: 69
Views: 67021
Reputation: 3627
It still did not work until i pasted this where i had module.id, on top of component. like this
declare var module: NodeModule;
interface NodeModule
{
id: string;
}@Component({module.id})
Upvotes: 0
Reputation: 30995
For me, I had a tsconfig.app.json
that extended tsconfig.json
. So when I added "types": ["node"]
in tsconfig.json
, it was being overridden by the "types" property in tsconfig.app.json
. Adding "node" to the existing "types" property in tsconfig.app.config
fixed it.
Upvotes: 1
Reputation: 73
I installed in the project, and worked well
npm install @types/node --save-dev
Upvotes: 0
Reputation: 61
module.id
cannot find the name module.
Follow following steps to resolves this issue,
step 1: Install node module by using the below command
npm i @types/node --save
Step 2: modify the file tsconfig.app.json
under src/app
"types": [
"node"
]
Upvotes: 6
Reputation: 214017
Update
If you use Typescript 2^ just use the following command:
npm i @types/node --save-dev
(instead of --save-dev
you can just use shortcut -D
)
or install it globally:
npm i @types/node --global
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.
Old version
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
Then your typings.json file will look like this:
"globalDependencies": {
"node": "registry:dt/node#6.0.0+20160608110640"
}
Upvotes: 102
Reputation: 2223
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"]
under compilerOptions
in your tsconfig.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" ]
under compilerOptions
in your tsconfig.json
file. This is instead of the types
property and has the benefit of automatically including any @types
you install with npm.For example:
{
"compilerOptions": {
"typeRoots": [
"node_modules/@types"
]
}
}
[Second edit]
Apparently, with the latest typescript, you only need typeRoots
or types
if tsconfig.json
is not in the root of your project or your types are not stored in node_modules/@types
.
Upvotes: 49
Reputation: 1231
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:
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'app works!';
}
Removing all occurances resolved my errors.
Upvotes: 14
Reputation: 860
This is what I did that worked for me on Eclipse(Webclipse)/Windows.
Step 1:
Terminal
$ npm install @types/node --global --save
Step 2:
tsconfig.json
{
"compilerOptions": {
...,
"types": ["node"]
}
}
In addition, I had the following dependencies in my package.json, so I was using typescript 2.
"devDependencies": {
...
"typescript": "~2.0.10",
"@types/node": "^6.0.46",
...
},
Upvotes: 1
Reputation: 51
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:
typings install // don't forget to install typings globally
then
npm install -D @types/node --save
in the package.json I have
"devDependencies": {
"@types/node": "6.0.40",
...
in the typings.json I have the following configuration
{
"compilerOptions": {
"target": "es5",
"module":"commonjs",
"moduleResolution": "node",
"sourceMap": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"removeComments": true,
"noImplicitAny": true,
"suppressImplicitAnyIndexErrors": true,
"allowSyntheticDefaultImports": true,
"types": []
},
"exclude": [
"node_modules",
"app-dist"
]
}
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 :)
Upvotes: 1
Reputation: 8934
Two key points:
Register typings by running typings install dt~node --global --save
. So you'll get the following section in typings.json
:
"globalDependencies": {
"node": "registry:dt/node#6.0.0+20160608110640"
}
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 the files
section of the tsconfig.json
{
"files": [
"typings/index.d.ts"
]
}
See more here.
Upvotes: 3
Reputation: 7640
Instead of "ambient" try "global" by Typings 1.0
typings install dt~node --global --save
Upvotes: 11