【问题标题】:Typescript Angular2 error cannot find module angular/xTypescript Angular2 错误找不到模块 angular/x
【发布时间】:2016-02-16 09:59:34
【问题描述】:

此应用使用 jspm 加载 javascript 模块,因此所有 angular2 typescript 定义文件都在 jspm_modules 文件夹中。我收到 Typescript 找不到模块 'angular/core'、'angular/route'、'angular/http' 等的错误。我在 VS 2015 编辑器和运行使用 gulp-typescript 的构建时都收到错误.我想可能是因为我的 tsconfig.json 中有 jspm_packages:

{
  "version": 3,
  "compilerOptions": {
    "target": "es5",
    "module": "system",
    "moduleResolution": "node",
    "sourceMap": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "removeComments": false,
    "noImplicitAny": false
  },
  "exclude": [
    "node_modules",
    "wwwroot/jspm_packages",
    "wwwroot/app"
  ]
}

但如果我删除该行,我会收到各种重复名称错误。我已经尝试按照其他类似问题中的建议编辑我的 xproj 文件,但无济于事。任何想法如何解决这个问题?

源代码在github上。

https://github.com/ryanlangton/angular2-asp5-starter/tree/typescript-errors

【问题讨论】:

  • 我认为package.json 目录中的npm install 应该可以解决这个问题。 TypeScript Compiler 会在你的 node_modules 中自动找到类型定义

标签: typescript visual-studio-2015 angular


【解决方案1】:

我认为package.json 目录中的npm install 应该可以解决此问题。 TypeScript 编译器会自动在你的node_modules 中找到类型定义。

问题是依赖解析在 JSPM 驱动的 Angualr2 应用程序中是双重的。

编译阶段,在本例中为gulp-typescript,会发现Angular2 模块符合typescript 编译器的规则。 tsc 通常会在 node_modules 中键入文件。所以如果你在本地通过npm安装Angular2,gulp-typescript会正确解析。

运行时阶段是完全不同的故事。在浏览器中,所有依赖都由 jspm 解析,由config.js 配置。您的打字稿被编译为 javascript,因此 tsc 不会在这里干扰。 Systemjs 会在jspm_packages 中找到angular2

所以,您的编译阶段依赖项似乎没有安装。试试npm install

【讨论】:

  • 很遗憾,jspm 不兼容 npm :(。通常你需要在 jspm 和 npm 中安装一个模块两次
猜你喜欢
  • 2017-08-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-16
  • 1970-01-01
  • 2017-09-10
  • 1970-01-01
  • 2016-08-10
相关资源
最近更新 更多