【问题标题】:Cannot find module 'angular2/core' in Visual Studio 2015 Update 1在 Visual Studio 2015 Update 1 中找不到模块“angular2/core”
【发布时间】:2016-03-22 15:20:40
【问题描述】:

我在 Visual Studio 2015 Update 1 中试用 angular2 beta 版本时遇到了这个错误Cannot find module 'angular2/core' .

代码如下:

import {Component} from "angular2/core"

@Component({
    selector: 'my-app',
    template: '<h1>My First Angular 2 App</h1>'
})
export class AppComponent {
}

我的 Visual Studio ASP.NET 5 项目中有 angular2 beta 版本。我在构建项目时也启用了编译 TS 文件。

我尝试了一些方法,但都没有成功:

  1. 我在 ts 文件的顶部添加了/// &lt;reference path="../node_modules/angular2/core.d.ts" /&gt;

2.

import {Component} from "angular2/bundles/angular2"

@Component({
    selector: 'my-app',
    template: '<h1>My First Angular 2 App</h1>'
})
export class AppComponent {
}

3.

import {Component} from "../node_modules/angular2/core"

@Component({
    selector: 'my-app',
    template: "<h1>My First Angular 2 App</h1>"
})
export class AppComponent {
}

更新:

这是我的 tsconfig.json

{
  "compilerOptions": {
    "noImplicitAny": false,
    "noEmitOnError": true,
    "removeComments": true,
    "sourceMap": true,
    "target": "es6",
    "module": "system",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true
  },
  "exclude": [
    "node_modules",
    "wwwroot",
    "bower_components"
  ]
}

更新:

我将 "moduleResolution": "node" 添加到 tsconfig.json 文件中,但现在又出现一组错误:

1>----- 重建所有开始:项目:Terminal.Web,配置: 调试任何 CPU ------ 1>C:\Test\node_modules\angular2\typings\es6-shim\es6-shim.d.ts(6,14): 错误 TS2300:构建:重复标识符“PropertyKey”。 1>C:\Test\node_modules\angular2\typings\es6-shim\es6-shim.d.ts(9,5): 错误 TS2300:构建:重复标识符“完成”。 1>C:\Test\node_modules\angular2\typings\es6-shim\es6-shim.d.ts(10,5): 错误 TS2300:构建:重复标识符“值”。 1>C:\Test\node_modules\angular2\typings\es6-shim\es6-shim.d.ts(248,5): 错误 TS2300:构建:重复标识符“EPSILON”。 1>C:\Test\node_modules\angular2\typings\es6-shim\es6-shim.d.ts(283,5): 错误 TS2300:构建:重复标识符“MAX_SAFE_INTEGER”。 1>C:\Test\node_modules\angular2\typings\es6-shim\es6-shim.d.ts(290,5): 错误 TS2300:构建:重复标识符“MIN_SAFE_INTEGER”。 1>C:\Test\node_modules\angular2\typings\es6-shim\es6-shim.d.ts(346,5): 错误 TS2300:构建:重复的标识符“标志”。 1>C:\Test\node_modules\angular2\typings\es6-shim\es6-shim.d.ts(498,5): 错误 TS2300:构建:重复标识符“原型”。 1>C:\Test\node_modules\angular2\typings\es6-shim\es6-shim.d.ts(561,5): 错误 TS2300:构建:重复标识符“大小”。 1>C:\Test\node_modules\angular2\typings\es6-shim\es6-shim.d.ts(570,5): 错误 TS2300:构建:重复标识符“原型”。 1>C:\Test\node_modules\angular2\typings\es6-shim\es6-shim.d.ts(581,5): 错误 TS2300:构建:重复标识符“大小”。 1>C:\Test\node_modules\angular2\typings\es6-shim\es6-shim.d.ts(590,5): 错误 TS2300:构建:重复标识符“原型”。 1>C:\Test\node_modules\angular2\typings\es6-shim\es6-shim.d.ts(605,5): 错误 TS2300:构建:重复标识符“原型”。 1>C:\Test\node_modules\angular2\typings\es6-shim\es6-shim.d.ts(619,5): 错误 TS2300:构建:重复标识符“原型”。 1>C:\程序 文件 (x86)\Microsoft SDKs\TypeScript\1.7\lib.es6.d.ts(3841,14):错误 TS2300:构建:重复标识符“PropertyKey”。 1>C:\程序文件 (x86)\Microsoft SDKs\TypeScript\1.7\lib.es6.d.ts(4061,5):错误 TS2300:构建:重复标识符“EPSILON”。 1>C:\程序文件 (x86)\Microsoft SDKs\TypeScript\1.7\lib.es6.d.ts(4096,5):错误 TS2300:构建:重复标识符“MAX_SAFE_INTEGER”。 1>C:\程序 文件 (x86)\Microsoft SDKs\TypeScript\1.7\lib.es6.d.ts(4103,5):错误 TS2300:构建:重复标识符“MIN_SAFE_INTEGER”。 1>C:\程序 文件 (x86)\Microsoft SDKs\TypeScript\1.7\lib.es6.d.ts(4389,5):错误 TS2300:构建:重复标识符“完成”。 1>C:\程序文件 (x86)\Microsoft SDKs\TypeScript\1.7\lib.es6.d.ts(4390,5):错误 TS2300:构建:重复的标识符“值”。 1>C:\程序文件 (x86)\Microsoft SDKs\TypeScript\1.7\lib.es6.d.ts(4619,5):错误 TS2300:构建:重复的标识符“标志”。 1>C:\程序文件 (x86)\Microsoft SDKs\TypeScript\1.7\lib.es6.d.ts(4647,5):错误 TS2300:构建:重复标识符“大小”。 1>C:\程序文件 (x86)\Microsoft SDKs\TypeScript\1.7\lib.es6.d.ts(4657,5):错误 TS2300:构建:重复标识符“原型”。 1>C:\程序文件 (x86)\Microsoft SDKs\TypeScript\1.7\lib.es6.d.ts(4674,5):错误 TS2300:构建:重复标识符“原型”。 1>C:\程序文件 (x86)\Microsoft SDKs\TypeScript\1.7\lib.es6.d.ts(4686,5):错误 TS2300:构建:重复标识符“大小”。 1>C:\程序文件 (x86)\Microsoft SDKs\TypeScript\1.7\lib.es6.d.ts(4696,5):错误 TS2300:构建:重复标识符“原型”。 1>C:\程序文件 (x86)\Microsoft SDKs\TypeScript\1.7\lib.es6.d.ts(4712,5):错误 TS2300:构建:重复标识符“原型”。 1>C:\程序文件 (x86)\Microsoft SDKs\TypeScript\1.7\lib.es6.d.ts(5099,5):错误 TS2300:构建:重复标识符“原型”。 ==========全部重建:0成功,1失败,0跳过==========

【问题讨论】:

  • 尝试在您的 tsconfig 中添加 "moduleResolution" : "node"
  • 我编译了相同的示例,但使用了 Code 和 npm。它工作正常,您可以发布您的 tsconfig.json 文件吗?
  • 检查您在 index.html 中导入 js 文件的顺序我遇到了同样的问题,但是在阅读了这个 repo github.com/pkozlowski-opensource/ng2-play/blob/master/… 后我已经解决了我的问题。希望它能帮助您找到错误
  • 第一:如果你还没有集成 system.import 那么你将如何设置引导文件(主组件)的入口点,第二:检查你的 node_modules 包中的 Core、common 等模块angular2 文件夹。
  • 这个answer 的另一个stackoverflow 问题解决了这个问题。

标签: typescript angular


【解决方案1】:

typescript 已经取得了长足的进步,Visual Studio 2015 Update 3 现已推出 typescript 2.0 版。

我发现 tsconfig.json 中的任何语法错误都会导致同样的问题。我在 compilerOptions 的最后一个键的末尾有一个逗号,这导致 ts 文件无法正确找到模块。

【讨论】:

  • 同意。我必须将 Visual Studio 更新到 Update 3,然后从 Tool > Extensions and Updates > Online 安装 Typescript 2.2。
【解决方案2】:

在您的 typings 文件夹下找到 browser.d.ts 文件,然后将其拖入您的 app.ts 或项目中的任何 ts 文件中。 你会得到 ///reference xxxx,应该解决一些错误

【讨论】:

    【解决方案3】:

    我有同样的问题。我不知道究竟是什么伎俩。

    • 使用 npm 安装 Angular:
      npm install Angular2
    • 更新了 Visual Studio 扩展:
      DotNetCore.1.0.0.RC2-VS2015Tools.Preview1

    【讨论】:

      猜你喜欢
      • 2016-03-10
      • 2016-08-10
      • 1970-01-01
      • 2018-06-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-25
      • 1970-01-01
      相关资源
      最近更新 更多