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

前端 未结 11 1419
梦毁少年i
梦毁少年i 2020-11-30 03:13

I have defined the following Angular2 component:

import {Component} from \'angular2/core\';

@Component({
  selector: \'my-app\',
  moduleId: module.id,
  te         


        
11条回答
  •  北海茫月
    2020-11-30 03:18

    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.

提交回复
热议问题