programing

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

css3 2023. 7. 24. 22:42

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

다음 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의 JS 모듈 시스템:

{
  "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 오류를 수정하려면 어떻게 해야 합니까?

갱신하다

Typescript 2^을 사용하는 경우 다음 명령을 사용합니다.

npm i @types/node --save-dev

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

또는 전체적으로 설치합니다.

npm i @types/node --global

다음을 지정할 수도 있습니다.typeRoots또는types원하는 경우 tsconfig.json에 포함되지만 기본적으로 표시되는 모든 "@types" 패키지가 컴파일포함됩니다.

이전 버전

노드 주변 종속성을 설치해야 합니다.타이핑.json

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

타이핑 관리자를 사용하여 노드 정의 파일을 전체적으로 설치할 수도 있습니다.

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

그러면 typing.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단계 지침을 찾는 데 어려움을 겪었습니다.

참조:활자본 발행 9184

편집:

다음 작업도 수행할 수 있습니다.

  1. 포함하다"typeRoots": [ "node_modules/@types" ]아래compilerOptions당신의tsconfig.json파일. 이것은 대신에.types자산 및 자동으로 포함되는 이점이 있습니다.@typesnpm을 사용하여 설치합니다.

예:

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

[2차 편집]보아하니, 최신 유형의 스크립트를 사용할 때, 당신이 필요합니다.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!';
}

모든 항목을 제거하여 오류를 해결했습니다.

"주변" 대신 Typing 1.0의 "글로벌"을 시도합니다.

typings install dt~node --global --save

module.id

이름 모듈을 찾을 수 없습니다.

이 문제를 해결하려면 다음 단계를 수행합니다.

1단계: 아래 명령을 사용하여 노드 모듈을 설치합니다.

npm i @types/node --save

2단계: 파일 수정tsconfig.app.json아래src/app

"types": [
    "node"
]

두 가지 핵심 사항:

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

자세한 내용은 여기를 참조하십시오.

VS 2015를 사용하고 있으며 동일한 문제가 있었지만 다음을 사용하여 해결했습니다.

  1. angular.io 웹 사이트의 typings.json 파일(현재 2.0.0 final)을 추가하고 다음을 실행합니다.

    typings install // don't forget to install typings globally
    

그리고나서

npm install -D @types/node --save

꾸러미 속에.내가 가진 json

"devDependencies": {
"@types/node": "6.0.40",
...
  1. typing.json에 다음과 같은 구성이 있습니다.

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

유형을 빈 배열로 추가해야 했습니다.

  1. 중복 여부를 확인하고 moduleId: module.id 이 여전히 강조 표시되어 있는지 확인합니다.

p.s. 개인적으로 이상한 문제입니다. 왜냐하면 typing.json 내부의 타이핑을 제외하자마자 바로 '하이브리드'를 강조 표시했지만, 입력하면 중복되는 내용이 많기 때문입니다.누구를 탓해야 할지 모르겠어요, 저, 타자기나 시각 스튜디오입니다 :)

Eclipse(Webclipse)/Windows에서 사용할 수 있는 기능입니다.

1단계:

터미널

$ npm install @types/node --global --save

2단계:

tsconfig.json

{
  "compilerOptions": {
    ...,
    "types": ["node"]
  }
}

또한, 저는 제 패키지에 다음과 같은 종속성이 있었습니다.Json, 그래서 저는 2번 타자기를 사용했습니다.

  "devDependencies": {
    ...
    "typescript": "~2.0.10",
    "@types/node": "^6.0.46",
    ...
  },

나는, 나는이 요.tsconfig.app.json 연장된 것tsconfig.json그래서 제가 추가했을 때."types": ["node"]tsconfig.json▁was▁being▁it▁▁"▁▁by의 "types" 속성에 의해 재정의되었습니다.tsconfig.app.json " 형노 " 드에 " "의 기존 "types"tsconfig.app.config고쳤습니다.

저는 프로젝트에 설치했고, 일을 잘 했습니다.

npm install @types/node --save-dev

module.id 를 구성 요소 위에 붙여넣기 전까지는 작동하지 않았습니다.이것처럼.

var module: var 듈언: Module; 노드모;
NodeModule 파일 이름
{
id: 문열자;
}

@구성 요소({module.id })

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