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

前端 未结 11 1388
梦毁少年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:13

    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",
        ...
      },
    
    0 讨论(0)
  • 2020-11-30 03:14

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

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-30 03:18

    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.

    0 讨论(0)
  • 2020-11-30 03:26

    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"
    ]
    
    0 讨论(0)
  • 2020-11-30 03:30

    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.

    0 讨论(0)
提交回复
热议问题