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에서 이 작업을 수행하려는 사용자는 다음과 같이 해야 합니다.
npm install -D @types/node
- 포함하다
types: ["node"]
아래compilerOptions
당신의tsconfig.json
파일.
2단계 지침을 찾는 데 어려움을 겪었습니다.
참조:활자본 발행 9184
편집:
다음 작업도 수행할 수 있습니다.
- 포함하다
"typeRoots": [ "node_modules/@types" ]
아래compilerOptions
당신의tsconfig.json
파일. 이것은 대신에.types
자산 및 자동으로 포함되는 이점이 있습니다.@types
npm을 사용하여 설치합니다.
예:
{
"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"
]
두 가지 핵심 사항:
실행하여 유형 등록
typings install dt~node --global --save
다음 섹션을 참조하십시오.typings.json
:"globalDependencies": { "node": "registry:dt/node#6.0.0+20160608110640" }
새 모듈에 참조를 추가합니다.두 가지 방법:
TS의 종속성에 대한 참조를 직접 추가합니다.
/// <reference path="../../../typings/globals/node/index.d.ts" />
더하다
typings/index.d.ts
에서files
의 한 부분tsconfig.json
{ "files": [ "typings/index.d.ts" ] }
자세한 내용은 여기를 참조하십시오.
VS 2015를 사용하고 있으며 동일한 문제가 있었지만 다음을 사용하여 해결했습니다.
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",
...
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" ] }
유형을 빈 배열로 추가해야 했습니다.
- 중복 여부를 확인하고 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
'programing' 카테고리의 다른 글
wkhtmltopdf Docker 이미지 설치 및 실행 방법 (0) | 2023.07.24 |
---|---|
영숫자가 아닌 모든 문자, 새 줄 및 여러 공백을 하나의 공백으로 바꾸기 (0) | 2023.07.24 |
HTML 특수 문자를 제거하는 방법은 무엇입니까? (0) | 2023.07.24 |
.용 오라클 데이터 공급자.NET: 연결 요청 시간이 초과되었습니다. (0) | 2023.07.24 |
imdb.load_data() 함수에 대한 'allow_pickle=False일 때 객체 배열을 로드할 수 없음'을 수정하는 방법은 무엇입니까? (0) | 2023.07.24 |