ng build --prod报错:Module build failed: TypeError: Cannot read property 'type' of undefined
Angular CLI 1.5.3,构建产品包时报错:
> ng build --prod
.....
ERROR in ./node_modules/rxjs/observable/BoundNodeCallbackObservable.js
Module build failed: TypeError: Cannot read property 'type' of undefined
at Object.getEffectiveTypeAnnotationNode (D:\webstormProjects\xxx\node_modules\typescript\lib\typescript.js:9341:17)
at assignContextualParameterTypes (D:\webstormProjects\xxx\node_modules\typescript\lib\typescript.js:41652:25)
at checkFunctionExpressionOrObjectLiteralMethod (D:\webstormProjects\xxx\node_modules\typescript\lib\typescript.js:41948:29)
at checkExpressionWorker (D:\webstormProjects\xxx\node_modules\typescript\lib\typescript.js:42959:28)
at checkExpression (D:\webstormProjects\xxx\node_modules\typescript\lib\typescript.js:42898:42)
at checkExpressionCached (D:\webstormProjects\xxx\node_modules\typescript\lib\typescript.js:42779:38)
at checkReturnStatement (D:\webstormProjects\xxx\node_modules\typescript\lib\typescript.js:45418:54)
at checkSourceElement (D:\webstormProjects\xxx\node_modules\typescript\lib\typescript.js:46763:28)
at Object.forEach (D:\webstormProjects\xxx\node_modules\typescript\lib\typescript.js:1506:30)
at checkBlock (D:\webstormProjects\xxx\node_modules\typescript\lib\typescript.js:44563:16)
at checkSourceElement (D:\webstormProjects\xxx\node_modules\typescript\lib\typescript.js:46742:28)
at checkFunctionExpressionOrObjectLiteralMethodDeferred (D:\webstormProjects\xxx\node_modules\typescript\lib\typescript.js:41990:21)
at checkDeferredNodes (D:\webstormProjects\xxx\node_modules\typescript\lib\typescript.js:46828:25)
at checkSourceFileWorker (D:\webstormProjects\xxx\node_modules\typescript\lib\typescript.js:46863:17)
at checkSourceFile (D:\webstormProjects\xxx\node_modules\typescript\lib\typescript.js:46842:13)
at Object.forEach (D:\webstormProjects\xxx\node_modules\typescript\lib\typescript.js:1506:30)
@ ./node_modules/rxjs/observable/bindNodeCallback.js 2:36-76
@ ./node_modules/rxjs/add/observable/bindNodeCallback.js
@ ./node_modules/rxjs/Rx.js
@ ./src/app/service/home.service.ts
@ ./src/app/app.module.ngfactory.js
@ ./src/main.ts
@ multi ./src/main.ts
angular cli使用的软件包版本:
Angular CLI: 1.5.3
Node: 6.9.5
OS: win32 x64
Angular: 5.0.2
... animations, common, compiler, compiler-cli, core, forms
... http, language-service, platform-browser
... platform-browser-dynamic, router
@angular/cli: 1.5.3
@angular-devkit/build-optimizer: 0.0.33
@angular-devkit/core: 0.0.20
@angular-devkit/schematics: 0.0.36
@ngtools/json-schema: 1.1.0
@ngtools/webpack: 1.8.3
@schematics/angular: 0.1.5
typescript: 2.4.2
webpack: 3.8.1
解决方法
这个错误是出现在引入RX的Subject处。
原来引入Subject
import { Subject } from 'rxjs'
修改为
import { Subject } from 'rxjs/Subject'
类似的引入RX的类包括:
- Observable
- Subject
- Subscription
- ...