简体   繁体   中英

Typescript Angular2 error cannot find module angular/x

This app uses jspm for javascript module loading, so all of the angular2 typescript definition files are in jspm_modules folder. I get errors that Typescript cannot find module 'angular/core', 'angular/route', 'angular/http', etc. I get the error both in VS 2015 editor and when I run my build, which is using gulp-typescript. I thought maybe it's because I have jspm_packages in my tsconfig.json:

{
  "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"
  ]
}

But if I remove that line, I get all kinds of duplicate names errors. I've tried editing my xproj file as suggested in other similar questions, but to no avail. Any ideas how to fix this problem?

在此处输入图片说明

The source code is available on github.

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

I think an npm install in the package.json directory should fix this. TypeScript Compiler will find type definition automatically in your node_modules .

The problem is that dependency resolution is two-folded in a JSPM powered Angualr2 apps.

The compiling phase , which is gulp-typescript in this case, will find Angular2 module accroding to typescript compiler's rule. tsc will conventionally follow typing files in node_modules . So if you install Angular2 by npm locally, gulp-typescript will resolve it correctly.

The runtime phase is totally a different story, though. In browser all dependencies are resolved by jspm, which configured by config.js . Your typescript is compiled to javascript, so tsc does not interfere here. Systemjs will find angular2 in jspm_packages .

So, it seems your compiling phase dependencies are not installed. Try with npm install ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM