Typescript编译器错误TS2307:从无NPM的CDN加载时找不到模块“angular2 / core”
我有一个使用typescript的angular2应用程序的例子, 但没有在我的项目中的所有node_modules ( 像这样 )。 必要的文件embedded在我的index.html
的head
( 如示例1中所述 ):
<script src="https://cdnjs.cloudflare.com/ajax/libs/es6-shim/0.35.0/es6-shim.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.16/system-polyfills.js"></script> <script src="https://npmcdn.com/angular2/es6/dev/src/testing/shims_for_IE.js"></script> <script src="https://code.angularjs.org/tools/system.js"></script> <script src="https://code.angularjs.org/tools/typescript.js"></script> <script src="https://code.angularjs.org/2.0.0-beta.13/angular2-polyfills.js"></script> <script src="https://code.angularjs.org/2.0.0-beta.13/Rx.js"></script> <script src="https://code.angularjs.org/2.0.0-beta.13/angular2.dev.js"></script>
由于我想预编译我的打字稿文件,我安装了node.js
和tsc
。 我的编辑器(PHPStorm 2016.1)自动将文件编译为tsconfig.json
定义的assets/js
:
{ "compilerOptions": { "target": "es5", "module": "system", "declaration": true, "noImplicitAny": false, "preserveConstEnums": true, "removeComments": true, "outDir": "../assets/js", "sourceMap": true, "emitDecoratorMetadata": true, "experimentalDecorators": true, "emitBOM": true, "moduleResolution": "node" } }
应用程序加载( 如此处所述 ):
System.config({ packages: { 'assets/js': { format: 'register', defaultExtension: 'js' } } }); System.import( 'assets/js/main' ).then( null, console.error.bind( console ) );
在编译时,我总是得到(经典的)错误
error TS2307: Cannot find module 'angular2/core'
PHPStorm告诉我,它不能解决import:
即使所有的外部库都被加载:
但编译后(即使有上述错误),应用程序在浏览器中工作正常。
可能缺less打字稿定义(TSD)( 链接在这里 ),但tsd已被弃用。 我读了关于typestypings install angular --ambient
但我不知道这是否真的有必要(并抛出了一堆像这样的其他错误)。
所以我的问题是:
这只是我的设置(没有node_modules),我可以忽略的副作用,或者我必须安装其他的东西?
我的环境:
- 节点5.4.1
- npm 3.3.12
- tsc 1.8.9
- Mac OS X 10.11.4
- PHPStorm 2016.1
我有点失落。 任何提示将不胜感激。
你有这个问题,因为TypeScript编译器可以findAngular2 .d.ts
文件。 在使用NPM安装Angular2时提供了它们(请参阅文件夹node_modules/angular2
下的文件node_modules/angular2
)。
在tsconfig.json
文件中设置module
属性时,编译器会尝试在node_modules
文件夹中find它们。
编译源代码时可以注意到这是一个问题,但是此代码将被编译,并且您的应用程序能够运行…
有类似typings和tsd的工具(已弃用)手动安装这种types,但Angular2不存在于相应的registry中。