简体   繁体   English

Angular2 代码中的 TypeScript 错误:找不到名称“模块”

[英]TypeScript error in Angular2 code: Cannot find name 'module'

I have defined the following Angular2 component:我定义了以下 Angular2 组件:

import {Component} from 'angular2/core';

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

When I try to compile this, I get the following error on line 5:当我尝试编译它时,我在第 5 行收到以下错误:

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

I believe module.id is referring to the CommonJS module variable (see here ).我相信 module.id 指的是 CommonJS module变量(见这里)。 I have specified the CommonJS module system in tsconfig.json:我在 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
}

How can I correct the TypeScript error?如何更正 TypeScript 错误?

Update更新

If you use Typescript 2^ just use the following command:如果您使用Typescript 2^只需使用以下命令:

npm i @types/node --save-dev

(instead of --save-dev you can just use shortcut -D ) (而不是--save-dev你可以只使用快捷方式-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 .如果需要,您还可以在 tsconfig.json 中指定typeRootstypes ,但默认情况下,所有可见的“@types”包都包含在您的编译中

Old version旧版本

You need to install node ambientDependencies.您需要安装节点ambientDependencies。 Typings.json打字文件.json

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

Another way can use typings manager to install node definition file globally:另一种方法可以使用typings manager全局安装节点定义文件:

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

Then your typings.json file will look like this:然后您的 Typings.json 文件将如下所示:

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

For those looking to do this with Typescript 2.x and @types, here's what you need to do:对于那些希望使用 Typescript 2.x 和 @types 执行此操作的人,您需要执行以下操作:

  1. npm install -D @types/node
  2. Include types: ["node"] under compilerOptions in your tsconfig.json file.包括types: ["node"]compilerOptionstsconfig.json文件。

I had a tough time finding the instructions for step 2.我很难找到第 2 步的说明。

Reference: Typescript issue 9184参考: Typescript 问题 9184

Edit:编辑:

You could also do:你也可以这样做:

  1. Include "typeRoots": [ "node_modules/@types" ] under compilerOptions in your tsconfig.json file.包括"typeRoots": [ "node_modules/@types" ]compilerOptionstsconfig.json文件。 This is instead of the types property and has the benefit of automatically including any @types you install with npm.这不是types属性,并且具有自动包含您使用 npm 安装的任何@types的好处。

For example:例如:

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

[Second edit] Apparently , with the latest typescript, you only need typeRoots or types if tsconfig.json is not in the root of your project or your types are not stored in node_modules/@types . [第二编辑] 显然,与最新的打字稿,你只需要typeRootstypes ,如果tsconfig.json是不是在你的项目的根目录或您的类型不存储在node_modules/@types

I hit this error when porting my @angular/angular2 Quickstart project into a new angular cli auto-generated project.将我的 @angular/angular2 Quickstart 项目移植到一个新的 angular cli 自动生成的项目时,我遇到了这个错误。

It seems that moduleId: module.id isn't needed anymore.似乎不再需要moduleId: module.id了。

This is the latest auto-generated component:这是最新的自动生成组件:

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

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

Removing all occurances resolved my errors.删除所有事件解决了我的错误。

Typings 1.0 尝试“全局”而不是“环境”

typings install dt~node --global --save

module.id

cannot find the name module.找不到名称模块。

Follow following steps to resolves this issue,请按照以下步骤解决此问题,

step 1: Install node module by using the below command第 1 步:使用以下命令安装节点模块

npm i @types/node --save

Step 2: modify the file tsconfig.app.json under src/app第二步:修改src/app下的tsconfig.app.json文件

"types": [
    "node"
]

Two key points:两个关键点:

  1. Register typings by running typings install dt~node --global --save .通过运行typings install dt~node --global --save注册typings install dt~node --global --save So you'll get the following section in typings.json :因此,您将在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直接在 TS 中添加对依赖项的引用

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

    • Add typings/index.d.ts in the files section of the tsconfig.jsontypings/index.d.tsfiles部分添加typings/index.d.ts tsconfig.json

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

See more here . 在此处查看更多信息

I use VS 2015, and had same issues, but I have resolved using:我使用 VS 2015,并且遇到了同样的问题,但我已经使用以下方法解决了:

  1. add the typings.json file from the angular.io website (2.0.0 final at the moment) and the run:从 angular.io 网站(目前为 2.0.0 final)添加typings.json 文件并运行:

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

then那么

npm install -D @types/node --save

in the package.json I have在 package.json 我有

"devDependencies": {
"@types/node": "6.0.40",
...
  1. in the typings.json I have the following configuration在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" ] }

I had to add the types as an empty array我不得不将类型添加为空数组

  1. check for duplicates, and if moduleId: module.id is still highlighted检查重复项,如果 moduleId: module.id 仍然突出显示

ps 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. ps 对我个人来说是一个奇怪的问题,因为一旦你在typings.json 中排除了类型,你就会立即突出显示“模块”,但是如果你让它进来,你就会有很多重复。 Don't know who to blame, me, typescript or visual studio :)不知道该怪谁,我,打字稿或视觉工作室:)

For me, I had a tsconfig.app.json that extended tsconfig.json .对于我来说,我有一个tsconfig.app.json该扩展tsconfig.json So when I added "types": ["node"] in tsconfig.json , it was being overridden by the "types" property in tsconfig.app.json .因此,当我在tsconfig.json添加"types": ["node"]时,它被tsconfig.json的 "types" 属性tsconfig.app.json Adding "node" to the existing "types" property in tsconfig.app.config fixed it.将“节点”添加到tsconfig.app.config现有的“类型”属性修复了它。

This is what I did that worked for me on Eclipse(Webclipse)/Windows.这就是我在 Eclipse(Webclipse)/Windows 上所做的工作。

Step 1:第 1 步:

Terminal终端

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

Step 2:第 2 步:

tsconfig.json配置文件

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

In addition, I had the following dependencies in my package.json, so I was using typescript 2.此外,我的 package.json 中有以下依赖项,因此我使用的是 typescript 2。

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

我安装在项目中,运行良好

npm install @types/node --save-dev

It still did not work until i pasted this where i had module.id, on top of component.它仍然没有工作,直到我将它粘贴到组件顶部我有 module.id 的地方。 like this像这样

declare var module: NodeModule;声明 var 模块:NodeModule;
interface NodeModule接口节点模块
{ {
id: string; id:字符串;
} }

@Component({module.id}) @Component({module.id})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Typescript Angular2错误找不到模块angular / x - Typescript Angular2 error cannot find module angular/x 在Angular2打字稿类中找不到模块 - Cannot find module in Angular2 typescript class 找不到模块Angular2 Typescript - Cannot find the module Angular2 Typescript IntelliJ和Angular2 / TypeScript-错误TS2307:找不到模块“ angular2 /…” - IntelliJ & Angular2 / TypeScript - error TS2307: Cannot find module 'angular2/…' Angular2 TypeScript - 错误TS2307:找不到模块&#39;angular2 / core&#39; - Angular2 TypeScript - error TS2307: Cannot find module 'angular2/core' angular2快速入门找不到名称“模块” - angular2 quickstart cannot find name 'module' 找不到外部模块&#39;angular2 / angular2&#39; - Angular2 w / Typescript - Cannot find external module 'angular2/angular2' - Angular2 w/ Typescript 打字稿编译错误TS2307:找不到带有angular2 rc5和ngModule的模块 - Typescript compilation error TS2307: Cannot find module with angular2 rc5 and ngModule Angular2组件相对路径Visual Studio 2015 module.id错误-找不到名称&#39;module&#39; - Angular2 Component Relative Paths Visual Studio 2015 module.id error - Cannot find name 'module' 带有angular2和咕unt声错误的打字稿TS2304:找不到名称 - Typescript with angular2 and grunt error TS2304: Cannot find name
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM