programing

Angular2 코드의 TypeScript 오류 : '모듈'이름을 찾을 수 없습니다.

nasanasas 2020. 11. 26. 08:27
반응형

Angular2 코드의 TypeScript 오류 : '모듈'이름을 찾을 수 없습니다.


다음 Angular2 구성 요소를 정의했습니다.

import {Component} from 'angular2/core';

@Component({
  selector: 'my-app',
  moduleId: module.id,
  templateUrl: './app.component.html'
})
export class AppComponent {
}

이것을 컴파일하려고하면 5 행에 다음과 같은 오류가 발생합니다.

src/app/app.component.ts(5,13): error TS2304: Cannot find name 'module'.

module.id가 CommonJS module변수를 참조한다고 생각 합니다 ( 여기 참조 ). tsconfig.json에 CommonJS 모듈 시스템을 지정했습니다.

{
  "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
}

TypeScript 오류를 어떻게 수정할 수 있습니까?


최신 정보

당신이 사용하는 경우 타이프 라이터 2 ^ 바로 다음 명령을 사용합니다 :

npm i @types/node --save-dev

(대신 --save-dev바로 가기를 사용할 수 있습니다 -D)

또는 전역으로 설치하십시오.

npm i @types/node --global

원하는 경우 typeRoots또는 typestsconfig.json에서 지정할 수도 있지만 기본적으로 보이는 모든 "@types"패키지가 컴파일에 포함됩니다 .

구 버전

nodeambientDependencies를 설치해야합니다. Typings.json

"ambientDependencies": {
    "node": "github:DefinitelyTyped/DefinitelyTyped/node/node.d.ts#138ad74b9e8e6c08af7633964962835add4c91e2",

또 다른 방법은 타이핑 관리자를 사용하여 노드 정의 파일을 전역으로 설치할 수 있습니다 .

typings install dt~node --global --save-dev

그러면 typings.json 파일은 다음과 같습니다.

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

Typescript 2.x 및 @types를 사용하여이 작업을 수행하려는 경우 다음을 수행해야합니다.

  1. npm install -D @types/node
  2. 파일에 types: ["node"]아래 compilerOptions포함 tsconfig.json하십시오.

2 단계의 지침을 찾기가 어려웠습니다.

참조 : Typescript 문제 9184

편집하다:

다음을 수행 할 수도 있습니다.

  1. 파일에 "typeRoots": [ "node_modules/@types" ]아래 compilerOptions포함 tsconfig.json하십시오. 이것은 types속성 대신에 @typesnpm으로 설치 한 모든 것을 자동으로 포함하는 이점이 있습니다.

예를 들면 :

{
  "compilerOptions": {
    "typeRoots": [
      "node_modules/@types"
    ]
  }
}

[두 번째 편집] 분명히 , 최신 타이프 라이터와 함께, 당신은 단지 필요 typeRoots하거나 types경우 tsconfig.json프로젝트의 루트에없는 또는 유형에 저장되지 않습니다 node_modules/@types.


@ angular / angular2 Quickstart 프로젝트를 새로운 angular cli 자동 생성 프로젝트로 이식 할 때이 오류가 발생했습니다.

moduleId: module.id더 이상 필요하지 않은 것 같습니다 .

다음은 최신 자동 생성 구성 요소입니다.

import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'app works!';
}

모든 발생을 제거하면 내 오류가 해결되었습니다.


"ambient"대신 Typings 1.0의 "global"을 사용해보십시오.

typings install dt~node --global --save

두 가지 핵심 사항 :

  1. 를 실행하여 타이핑을 등록합니다 typings install dt~node --global --save. 따라서 다음 섹션이 표시됩니다 typings.json.

    "globalDependencies": {
        "node": "registry:dt/node#6.0.0+20160608110640"
    }
    
  2. 새 모듈에 대한 참조를 추가하십시오. 두 가지 방법:

    • TS에서 종속성에 대한 참조를 직접 추가

      /// <reference path="../../../typings/globals/node/index.d.ts" />

    • 추가 typings/index.d.ts에서 files의 섹션tsconfig.json

      {
          "files": [
              "typings/index.d.ts"
          ]
      }
      

See more here.


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


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"
]

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

I installed in the project, and worked well

npm install @types/node --save-dev

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.

참고URL : https://stackoverflow.com/questions/36700693/typescript-error-in-angular2-code-cannot-find-name-module

반응형