Join the Stack Overflow Community
Stack Overflow is a community of 6.3 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

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?

share|improve this question
    
I don't think module is something that's provided to you at compile time. – toskv Apr 18 at 17:47
    
Well it is compiling correctly for me in this project. I don't know how!!! So I tried to isolate this in a simpler project and it is not working there. So trying to figure out what is making it work in the first project. – Naresh Apr 18 at 17:51
    
You can declare a module.d.ts file with the content: declare var module: any;. Then reference this file from your bootstrap /// <reference path="path/to/typings/module.d.ts" /> – PierreDuc Apr 18 at 17:52
1  
In typings.json ambientDependencies: node – yurzui Apr 18 at 17:56
    
@yurzui, you nailed it on the head! Can you please write it out as an answer so that I can mark it correct? – Naresh Apr 18 at 18:01
up vote 33 down vote accepted

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

Then your typings.json file will look like this:

"globalDependencies": {
  "node": "registry:dt/node#6.0.0+20160608110640"
}

If you use Typescript 2^ just use the following command:

npm install @types/node --global --save

Or install it within the project:

npm install @types/node --save

and then add a reference to it from your bootstrap:

/// <reference path="../../node_modules/@types/node/index.d.ts" />
share|improve this answer
3  
Two points: 1. Remember to add the reference to the top of your root typescript file thus: /// <reference path="../../typings/main/ambient/node/index.d.ts" /> 2. Editing the typings.json file as shown by @yurzui worked for me but initially I just did this: > typings install node --ambient --save Which updated the typings.json file [with a different version] and didn't work as the index.d.ts file threw scores of errors at transpile time. FYI: I'm using Angular Beta.16 – Bonneville Apr 27 at 16:41

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.

share|improve this answer
    
This is definitely an inane question, but does "under" mean "nested within"? I get an error nesting it within compilerOptions, no error placing it at the same level, but it would seem logically to belong as a child of compilerOptions? – msanford Oct 3 at 19:12
    
@msanford yes, nested within. I'll make the answer more explicit. – Isaac Oct 4 at 13:56

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

typings install dt~node --global --save
share|improve this answer

Two key points:

  1. 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"
    }
    
  2. 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.

share|improve this answer

I use VS 2015, and had same issues, but I have resolved using:

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

  1. check for duplicates, and if moduleId: module.id is still highlighted

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 :)

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.