Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

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 6 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"
}
share|improve this answer
    
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

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

typings install dt~node --global --save
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.