Angular2 构建错误 - 找不到类型
Posted
技术标签:
【中文标题】Angular2 构建错误 - 找不到类型【英文标题】:Angular2 build errors - cannot find types 【发布时间】:2016-11-01 18:56:36 【问题描述】:我在打字时遇到了一些问题。当我运行npm start
时,出现以下错误:
> tsc && concurrently "npm run tscw" "npm run lite"
node_modules/@angular/common/src/directives/ng_class.d.ts(72,35): error TS2304: Cannot find name 'Set'.
node_modules/@angular/common/src/facade/async.d.ts(27,33): error TS2304: Cannot find name 'Promise'.
node_modules/@angular/common/src/facade/async.d.ts(28,45): error TS2304: Cannot find name 'Promise'.
node_modules/@angular/common/src/facade/lang.d.ts(4,17): error TS2304: Cannot find name 'Map'.
node_modules/@angular/common/src/facade/lang.d.ts(5,17): error TS2304: Cannot find name 'Set'.
node_modules/@angular/common/src/facade/lang.d.ts(70,59): error TS2304: Cannot find name 'Map'.
node_modules/@angular/common/src/facade/promise.d.ts(2,14): error TS2304: Cannot find name 'Promise'.
node_modules/@angular/common/src/facade/promise.d.ts(8,32): error TS2304: Cannot find name 'Promise'.
node_modules/@angular/common/src/facade/promise.d.ts(9,38): error TS2304: Cannot find name 'Promise'.
node_modules/@angular/common/src/facade/promise.d.ts(10,35): error TS2304: Cannot find name 'Promise'.
node_modules/@angular/common/src/facade/promise.d.ts(10,93): error TS2304: Cannot find name 'Promise'.
node_modules/@angular/common/src/facade/promise.d.ts(11,34): error TS2304: Cannot find name 'Promise'.
node_modules/@angular/common/src/facade/promise.d.ts(11,50): error TS2304: Cannot find name 'Promise'.
node_modules/@angular/common/src/facade/promise.d.ts(12,32): error TS2304: Cannot find name 'Promise'.
node_modules/@angular/common/src/facade/promise.d.ts(12,149): error TS2304: Cannot find name 'Promise'.
node_modules/@angular/common/src/facade/promise.d.ts(13,43): error TS2304: Cannot find name 'Promise'.
node_modules/@angular/common/src/forms/directives/select_control_value_accessor.d.ts(18,17): error TS2304: Cannot find name 'Map'.
node_modules/@angular/common/src/pipes/async_pipe.d.ts(29,29): error TS2304: Cannot find name 'Promise'.
node_modules/@angular/common/src/pipes/async_pipe.d.ts(35,38): error TS2304: Cannot find name 'Promise'.
node_modules/@angular/common/src/pipes/async_pipe.d.ts(37,39): error TS2304: Cannot find name 'Promise'.
node_modules/@angular/common/src/pipes/async_pipe.d.ts(39,44): error TS2304: Cannot find name 'Promise'.
node_modules/@angular/core/src/application_ref.d.ts(39,88): error TS2304: Cannot find name 'Promise'.
node_modules/@angular/core/src/application_ref.d.ts(99,42): error TS2304: Cannot find name 'Promise'.
node_modules/@angular/core/src/application_ref.d.ts(174,33): error TS2304: Cannot find name 'Promise'.
node_modules/@angular/core/src/change_detection/differs/default_keyvalue_differ.d.ts(24,15): error TS2304: Cannot find name 'Map'.
node_modules/@angular/core/src/change_detection/differs/default_keyvalue_differ.d.ts(26,16): error TS2304: Cannot find name 'Map'.
node_modules/@angular/core/src/di/reflective_provider.d.ts(105,123): error TS2304: Cannot find name 'Map'.
node_modules/@angular/core/src/di/reflective_provider.d.ts(105,165): error TS2304: Cannot find name 'Map'.
node_modules/@angular/core/src/facade/async.d.ts(27,33): error TS2304: Cannot find name 'Promise'.
node_modules/@angular/core/src/facade/async.d.ts(28,45): error TS2304: Cannot find name 'Promise'.
node_modules/@angular/core/src/facade/collection.d.ts(1,25): error TS2304: Cannot find name 'MapConstructor'.
node_modules/@angular/core/src/facade/collection.d.ts(2,25): error TS2304: Cannot find name 'SetConstructor'.
node_modules/@angular/core/src/facade/collection.d.ts(4,27): error TS2304: Cannot find name 'Map'.
node_modules/@angular/core/src/facade/collection.d.ts(4,39): error TS2304: Cannot find name 'Map'.
node_modules/@angular/core/src/facade/collection.d.ts(7,9): error TS2304: Cannot find name 'Map'.
node_modules/@angular/core/src/facade/collection.d.ts(8,30): error TS2304: Cannot find name 'Map'.
node_modules/@angular/core/src/facade/collection.d.ts(11,43): error TS2304: Cannot find name 'Map'.
node_modules/@angular/core/src/facade/collection.d.ts(12,27): error TS2304: Cannot find name 'Map'.
node_modules/@angular/core/src/facade/collection.d.ts(14,23): error TS2304: Cannot find name 'Map'.
node_modules/@angular/core/src/facade/collection.d.ts(15,25): error TS2304: Cannot find name 'Map'.
node_modules/@angular/core/src/facade/collection.d.ts(100,41): error TS2304: Cannot find name 'Set'.
node_modules/@angular/core/src/facade/collection.d.ts(101,22): error TS2304: Cannot find name 'Set'.
node_modules/@angular/core/src/facade/collection.d.ts(102,25): error TS2304: Cannot find name 'Set'.
node_modules/@angular/core/src/facade/lang.d.ts(4,17): error TS2304: Cannot find name 'Map'.
node_modules/@angular/core/src/facade/lang.d.ts(5,17): error TS2304: Cannot find name 'Set'.
node_modules/@angular/core/src/facade/lang.d.ts(70,59): error TS2304: Cannot find name 'Map'.
node_modules/@angular/core/src/facade/promise.d.ts(2,14): error TS2304: Cannot find name 'Promise'.
node_modules/@angular/core/src/facade/promise.d.ts(8,32): error TS2304: Cannot find name 'Promise'.
node_modules/@angular/core/src/facade/promise.d.ts(9,38): error TS2304: Cannot find name 'Promise'.
node_modules/@angular/core/src/facade/promise.d.ts(10,35): error TS2304: Cannot find name 'Promise'.
node_modules/@angular/core/src/facade/promise.d.ts(10,93): error TS2304: Cannot find name 'Promise'.
node_modules/@angular/core/src/facade/promise.d.ts(11,34): error TS2304: Cannot find name 'Promise'.
node_modules/@angular/core/src/facade/promise.d.ts(11,50): error TS2304: Cannot find name 'Promise'.
node_modules/@angular/core/src/facade/promise.d.ts(12,32): error TS2304: Cannot find name 'Promise'.
node_modules/@angular/core/src/facade/promise.d.ts(12,149): error TS2304: Cannot find name 'Promise'.
node_modules/@angular/core/src/facade/promise.d.ts(13,43): error TS2304: Cannot find name 'Promise'.
node_modules/@angular/core/src/linker/component_resolver.d.ts(8,53): error TS2304: Cannot find name 'Promise'.
node_modules/@angular/core/src/linker/component_resolver.d.ts(12,44): error TS2304: Cannot find name 'Promise'.
node_modules/@angular/core/src/linker/dynamic_component_loader.d.ts(62,148): error TS2304: Cannot find name 'Promise'.
node_modules/@angular/core/src/linker/dynamic_component_loader.d.ts(103,144): error TS2304: Cannot find name 'Promise'.
node_modules/@angular/core/src/linker/dynamic_component_loader.d.ts(108,139): error TS2304: Cannot find name 'Promise'.
node_modules/@angular/core/src/linker/dynamic_component_loader.d.ts(109,135): error TS2304: Cannot find name 'Promise'.
node_modules/@angular/core/src/reflection/reflector.d.ts(28,22): error TS2304: Cannot find name 'Map'.
node_modules/@angular/core/src/reflection/reflector.d.ts(30,15): error TS2304: Cannot find name 'Map'.
node_modules/@angular/core/src/reflection/reflector.d.ts(32,15): error TS2304: Cannot find name 'Map'.
node_modules/@angular/core/src/reflection/reflector.d.ts(34,15): error TS2304: Cannot find name 'Map'.
node_modules/@angular/core/src/reflection/reflector.d.ts(36,16): error TS2304: Cannot find name 'Set'.
node_modules/@angular/core/src/testability/testability.d.ts(40,20): error TS2304: Cannot find name 'Map'.
node_modules/@angular/http/src/headers.d.ts(29,18): error TS2304: Cannot find name 'Map'.
node_modules/@angular/http/src/headers.d.ts(45,59): error TS2304: Cannot find name 'Map'.
node_modules/@angular/http/src/url_search_params.d.ts(11,16): error TS2304: Cannot find name 'Map'.
node_modules/@angular/platform-browser-dynamic/platform_browser_dynamic.d.ts(75,90): error TS2304: Cannot find name 'Promise'.
node_modules/@angular/router-deprecated/src/directives/router_outlet.d.ts(27,54): error TS2304: Cannot find name 'Promise'.
node_modules/@angular/router-deprecated/src/directives/router_outlet.d.ts(33,51): error TS2304: Cannot find name 'Promise'.
node_modules/@angular/router-deprecated/src/directives/router_outlet.d.ts(38,56): error TS2304: Cannot find name 'Promise'.
node_modules/@angular/router-deprecated/src/directives/router_outlet.d.ts(47,65): error TS2304: Cannot find name 'Promise'.
node_modules/@angular/router-deprecated/src/directives/router_outlet.d.ts(58,60): error TS2304: Cannot find name 'Promise'.
node_modules/@angular/router-deprecated/src/facade/async.d.ts(27,33): error TS2304: Cannot find name 'Promise'.
node_modules/@angular/router-deprecated/src/facade/async.d.ts(28,45): error TS2304: Cannot find name 'Promise'.
node_modules/@angular/router-deprecated/src/facade/lang.d.ts(4,17): error TS2304: Cannot find name 'Map'.
node_modules/@angular/router-deprecated/src/facade/lang.d.ts(5,17): error TS2304: Cannot find name 'Set'.
node_modules/@angular/router-deprecated/src/facade/lang.d.ts(70,59): error TS2304: Cannot find name 'Map'.
node_modules/@angular/router-deprecated/src/facade/promise.d.ts(2,14): error TS2304: Cannot find name 'Promise'.
node_modules/@angular/router-deprecated/src/facade/promise.d.ts(8,32): error TS2304: Cannot find name 'Promise'.
node_modules/@angular/router-deprecated/src/facade/promise.d.ts(9,38): error TS2304: Cannot find name 'Promise'.
node_modules/@angular/router-deprecated/src/facade/promise.d.ts(10,35): error TS2304: Cannot find name 'Promise'.
node_modules/@angular/router-deprecated/src/facade/promise.d.ts(10,93): error TS2304: Cannot find name 'Promise'.
node_modules/@angular/router-deprecated/src/facade/promise.d.ts(11,34): error TS2304: Cannot find name 'Promise'.
node_modules/@angular/router-deprecated/src/facade/promise.d.ts(11,50): error TS2304: Cannot find name 'Promise'.
node_modules/@angular/router-deprecated/src/facade/promise.d.ts(12,32): error TS2304: Cannot find name 'Promise'.
node_modules/@angular/router-deprecated/src/facade/promise.d.ts(12,149): error TS2304: Cannot find name 'Promise'.
node_modules/@angular/router-deprecated/src/facade/promise.d.ts(13,43): error TS2304: Cannot find name 'Promise'.
node_modules/@angular/router-deprecated/src/instruction.d.ts(125,34): error TS2304: Cannot find name 'Promise'.
node_modules/@angular/router-deprecated/src/instruction.d.ts(161,25): error TS2304: Cannot find name 'Promise'.
node_modules/@angular/router-deprecated/src/instruction.d.ts(179,34): error TS2304: Cannot find name 'Promise'.
node_modules/@angular/router-deprecated/src/instruction.d.ts(182,25): error TS2304: Cannot find name 'Promise'.
node_modules/@angular/router-deprecated/src/interfaces.d.ts(21,107): error TS2304: Cannot find name 'Promise'.
node_modules/@angular/router-deprecated/src/interfaces.d.ts(39,104): error TS2304: Cannot find name 'Promise'.
node_modules/@angular/router-deprecated/src/interfaces.d.ts(57,109): error TS2304: Cannot find name 'Promise'.
node_modules/@angular/router-deprecated/src/interfaces.d.ts(80,109): error TS2304: Cannot find name 'Promise'.
node_modules/@angular/router-deprecated/src/interfaces.d.ts(102,114): error TS2304: Cannot find name 'Promise'.
node_modules/@angular/router-deprecated/src/lifecycle/lifecycle_annotations.d.ts(29,100): error TS2304: Cannot find name 'Promise'.
node_modules/@angular/router-deprecated/src/route_config/route_config_impl.d.ts(105,19): error TS2304: Cannot find name 'Promise'.
node_modules/@angular/router-deprecated/src/route_definition.d.ts(20,20): error TS2304: Cannot find name 'Promise'.
node_modules/@angular/router-deprecated/src/route_definition.d.ts(35,20): error TS2304: Cannot find name 'Promise'.
node_modules/@angular/router-deprecated/src/route_registry.d.ts(52,66): error TS2304: Cannot find name 'Promise'.
node_modules/@angular/router-deprecated/src/router.d.ts(56,50): error TS2304: Cannot find name 'Promise'.
node_modules/@angular/router-deprecated/src/router.d.ts(68,46): error TS2304: Cannot find name 'Promise'.
node_modules/@angular/router-deprecated/src/router.d.ts(86,45): error TS2304: Cannot find name 'Promise'.
node_modules/@angular/router-deprecated/src/router.d.ts(99,34): error TS2304: Cannot find name 'Promise'.
node_modules/@angular/router-deprecated/src/router.d.ts(107,64): error TS2304: Cannot find name 'Promise'.
node_modules/@angular/router-deprecated/src/router.d.ts(112,85): error TS2304: Cannot find name 'Promise'.
node_modules/@angular/router-deprecated/src/router.d.ts(114,51): error TS2304: Cannot find name 'Promise'.
node_modules/@angular/router-deprecated/src/router.d.ts(116,72): error TS2304: Cannot find name 'Promise'.
node_modules/@angular/router-deprecated/src/router.d.ts(122,48): error TS2304: Cannot find name 'Promise'.
node_modules/@angular/router-deprecated/src/router.d.ts(128,70): error TS2304: Cannot find name 'Promise'.
node_modules/@angular/router-deprecated/src/router.d.ts(140,43): error TS2304: Cannot find name 'Promise'.
node_modules/@angular/router-deprecated/src/router.d.ts(144,29): error TS2304: Cannot find name 'Promise'.
node_modules/@angular/router-deprecated/src/router.d.ts(150,19): error TS2304: Cannot find name 'Promise'.
node_modules/@angular/router-deprecated/src/router.d.ts(162,70): error TS2304: Cannot find name 'Promise'.
node_modules/rxjs/Observable.d.ts(10,66): error TS2304: Cannot find name 'Promise'.
node_modules/rxjs/Observable.d.ts(66,60): error TS2304: Cannot find name 'Promise'.
node_modules/rxjs/Observable.d.ts(66,70): error TS2304: Cannot find name 'Promise'.
node_modules/rxjs/operator/toPromise.d.ts(7,59): error TS2304: Cannot find name 'Promise'.
node_modules/rxjs/operator/toPromise.d.ts(7,69): error TS2304: Cannot find name 'Promise'.
node_modules/rxjs/operator/toPromise.d.ts(9,9): error TS2304: Cannot find name 'Promise'.
node_modules/rxjs/operator/toPromise.d.ts(10,26): error TS2304: Cannot find name 'Promise'.
node_modules/rxjs/operator/toPromise.d.ts(10,36): error TS2304: Cannot find name 'Promise'.
src/classes/animal.ts(42,18): error TS2339: Property 'startsWith' does not exist on type 'string'.
其他人在过去几个月里遇到过这个问题,但这些修复都没有对我有用。
有什么想法吗? (角度 2)
【问题讨论】:
***.com/a/38876022/361714 【参考方案1】:如果您使用 --target=es5,则需要在应用程序的某处添加一行(例如,在您调用引导程序的 .ts 文件顶部):
<reference path="node_modules/angular2/typings/browser.d.ts"/>
看到这个帖子:Angular 2 typescript can't find names
我个人切换到了 ES6,到目前为止它工作得非常好。我还建议为 Angular2 做 5 分钟快速入门和教程系列,因为您可以将它用作稳定的骨架来进行进一步的项目: https://angular.io/guide/quickstart
祝你好运
【讨论】:
【参考方案2】:我最近在 Visual Studio 中遇到了类似的问题,我在 Google 上进行了很多搜索,但找不到任何关于在 Visual Studio 中运行 Web 应用程序时特别出现的问题的帖子。似乎我的应用程序甚至可以通过 npm 成功构建,尽管它不能在 VS 中。最终,经过几个晚上的工作,我找到了原因。我想在这里分享我的经验,因为在 Visual Studio 中似乎很少有帖子谈论这个问题。我希望这可以帮助那些在 VS 中不太熟悉 typescript 的人,因为我在搜索这个问题时很容易找到这篇文章。
基本上,VS中有打字稿工具,你可以在Tools->Extensions and Updates中看到它和它的版本。我的项目正是通过教程 VISUAL STUDIO 2015 QUICKSTART 创建的项目:https://angular.io/guide/visual-studio-2015。
事实上,我的 VS 的 typescript 扩展名为 1.8.xx,这与教程中要求的 2.2 不同。我认为 1.8.xx 是 VS 社区 2015 附带的默认 typescript 版本。我为解决问题所做的只是删除旧版本并在 VS 中重新安装 typescript 2.2。值得一提的是如何卸载它,因为卸载按钮在我的 VS 中被禁用。具体来说,转到文件夹 C:\ProgramData\Package Cache\,并在其中搜索 TypeScript_Dev14.msi。然后右键单击VS中的版本号之一将其卸载。在此之后,您应该不再在 VS 扩展中看到 typescript。然后,去 VS 中的 Extensions and Updates 搜索 typescript 2.2 并安装它。之后,我在 VS 中的构建运行正常。我在 Simon Weave 的评论中得到了这个想法。谢谢你救了我的命。 http://blog.stevensanderson.com/2016/10/04/angular2-template-for-visual-studio/
我在教程中仍然不明白的是它说安装 typescript 可以通过我实际执行的 nmp -g install 或通过下载并安装 Visual Studio 2015 的 TypeScript 2.2 来完成。但似乎前者没有不过可以工作,因为它不会更改 VS 中的打字稿工具。
【讨论】:
【参考方案3】:在您的tsconfig.json
文件中,您所要做的就是将 CompilerOptions 目标从“es5”更改为“es6”,这应该可以工作。
【讨论】:
以上是关于Angular2 构建错误 - 找不到类型的主要内容,如果未能解决你的问题,请参考以下文章
升级到 RC5 后,Angular2 通用错误找不到模块“@angular/compiler/src/template_parser”
Angular2 代码中的 TypeScript 错误:找不到名称“模块”