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

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

    I installed in the project, and worked well

    npm install @types/node --save-dev
    
    0 讨论(0)
  • 2020-11-30 03:34

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

    typings install dt~node --global --save
    
    0 讨论(0)
  • 2020-11-30 03:36

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

    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.

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

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

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