簡體   English   中英

Angular2 代碼中的 TypeScript 錯誤:找不到名稱“模塊”

[英]TypeScript error in Angular2 code: Cannot find name '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 中指定了 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 錯誤?

更新

如果您使用Typescript 2^只需使用以下命令:

npm i @types/node --save-dev

(而不是--save-dev你可以只使用快捷方式-D

或全局安裝:

npm i @types/node --global

如果需要,您還可以在 tsconfig.json 中指定typeRootstypes ,但默認情況下,所有可見的“@types”包都包含在您的編譯中

舊版本

您需要安裝節點ambientDependencies。 打字文件.json

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

另一種方法可以使用typings manager全局安裝節點定義文件:

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"]compilerOptionstsconfig.json文件。

我很難找到第 2 步的說明。

參考: Typescript 問題 9184

編輯:

你也可以這樣做:

  1. 包括"typeRoots": [ "node_modules/@types" ]compilerOptionstsconfig.json文件。 這不是types屬性,並且具有自動包含您使用 npm 安裝的任何@types的好處。

例如:

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

[第二編輯] 顯然,與最新的打字稿,你只需要typeRootstypes ,如果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!';
}

刪除所有事件解決了我的錯誤。

Typings 1.0 嘗試“全局”而不是“環境”

typings install dt~node --global --save

module.id

找不到名稱模塊。

請按照以下步驟解決此問題,

第 1 步:使用以下命令安裝節點模塊

npm i @types/node --save

第二步:修改src/app下的tsconfig.app.json文件

"types": [
    "node"
]

兩個關鍵點:

  1. 通過運行typings install dt~node --global --save注冊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.tsfiles部分添加typings/index.d.ts tsconfig.json

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

在此處查看更多信息

我使用 VS 2015,並且遇到了同樣的問題,但我已經使用以下方法解決了:

  1. 從 angular.io 網站(目前為 2.0.0 final)添加typings.json 文件並運行:

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

那么

npm install -D @types/node --save

在 package.json 我有

"devDependencies": {
"@types/node": "6.0.40",
...
  1. 在typings.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 仍然突出顯示

ps 對我個人來說是一個奇怪的問題,因為一旦你在typings.json 中排除了類型,你就會立即突出顯示“模塊”,但是如果你讓它進來,你就會有很多重復。 不知道該怪誰,我,打字稿或視覺工作室:)

對於我來說,我有一個tsconfig.app.json該擴展tsconfig.json 因此,當我在tsconfig.json添加"types": ["node"]時,它被tsconfig.json的 "types" 屬性tsconfig.app.json 將“節點”添加到tsconfig.app.config現有的“類型”屬性修復了它。

這就是我在 Eclipse(Webclipse)/Windows 上所做的工作。

第 1 步:

終端

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

第 2 步:

配置文件

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

此外,我的 package.json 中有以下依賴項,因此我使用的是 typescript 2。

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

我安裝在項目中,運行良好

npm install @types/node --save-dev

它仍然沒有工作,直到我將它粘貼到組件頂部我有 module.id 的地方。 像這樣

聲明 var 模塊:NodeModule;
接口節點模塊
{
id:字符串;
}

@Component({module.id})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM