TypeScript error in Angular2 code: Cannot find name 'module'

For those looking to do this with Typescript 2.x and @types, here's what you need to do:

  1. npm install -D @types/node
  2. Include 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:

  1. Include "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.


Instead of "ambient" try "global" by Typings 1.0

typings install dt~node --global --save

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.


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"
}