`ng build --prod` fails with `ERROR in TypeError: Cannot read property 'version' of null`

`ng build --prod` fails with `ERROR in TypeError: Cannot read property 'version' of null`

我无法使用 --prod 标志成功构建我的项目 [否则构建良好]。我可以临时将 maxMetadata && 添加到发生错误的 if 语句中以进行空检查并通过该点(metadata_reader.js 第 66 行),然后我得到一个 "real" 问题列表生产分配;但我想首先了解为什么会发生这种情况,看看是否有永久性解决方案。我设法找到了类似的问题,并尝试了很多其他人尝试过的问题,但它似乎并不完全相同,而且还没有找到解决办法。

版本:

Angular CLI: 1.6.5
Node: 8.9.4
OS: win32 x64
Angular: 5.2.3
... animations, common, compiler, compiler-cli, core, forms
... http, language-service, platform-browser
... platform-browser-dynamic, router

@angular/cli: 1.6.5
@angular-devkit/build-optimizer: 0.0.42
@angular-devkit/core: 0.0.29
@angular-devkit/schematics: 0.0.52
@ngtools/json-schema: 1.1.0
@ngtools/webpack: 1.9.5
@schematics/angular: 0.1.17
typescript: 2.7.1
webpack: 3.10.0

完整堆栈跟踪:

ERROR in TypeError: Cannot read property 'version' of null
    at readMetadataFile (C:\git\screenings-plus\node_modules\@angular\compiler-cli\src\transformers\metadata_reader.js:66:29)
    at Object.readMetadata (C:\git\screenings-plus\node_modules\@angular\compiler-cli\src\transformers\metadata_reader.js:35:25)
    at TsCompilerAotCompilerTypeCheckHostAdapter.getMetadataFor (C:\git\screenings-plus\node_modules\@angular\compiler-cli\src\transformers\compiler_host.js:425:34)
    at StaticSymbolResolver.getModuleMetadata (C:\git\screenings-plus\node_modules\@angular\compiler\bundles\compiler.umd.js:29726:62)
    at StaticSymbolResolver.hasDecorators (C:\git\screenings-plus\node_modules\@angular\compiler\bundles\compiler.umd.js:29426:46)
    at analyzeFile (C:\git\screenings-plus\node_modules\@angular\compiler\bundles\compiler.umd.js:31299:63)
    at AotCompiler._analyzeFile (C:\git\screenings-plus\node_modules\@angular\compiler\bundles\compiler.umd.js:30509:17)
    at AotCompiler.findGeneratedFileNames (C:\git\screenings-plus\node_modules\@angular\compiler\bundles\compiler.umd.js:30525:42)
    at Object.findGeneratedFileNames (C:\git\screenings-plus\node_modules\@angular\compiler-cli\src\transformers\program.js:397:82)
    at TsCompilerAotCompilerTypeCheckHostAdapter.getSourceFile (C:\git\screenings-plus\node_modules\@angular\compiler-cli\src\transformers\compiler_host.js:337:55)
    at findSourceFile (C:\git\screenings-plus\node_modules\typescript\lib\typescript.js:74915:29)
    at args (C:\git\screenings-plus\node_modules\typescript\lib\typescript.js:74852:85)
    at getSourceFileFromReferenceWorker (C:\git\screenings-plus\node_modules\typescript\lib\typescript.js:74825:34)
    at processSourceFile (C:\git\screenings-plus\node_modules\typescript\lib\typescript.js:74852:13)
    at processRootFile (C:\git\screenings-plus\node_modules\typescript\lib\typescript.js:74706:13)
    at C:\git\screenings-plus\node_modules\typescript\lib\typescript.js:73855:60

为了解决这个问题,我在 metadata_reader.js 的第 66 行放置了一个 console.log(maxMetadata.version); 并且能够找到问题所在,结果是一个元数据文件 [ *.metadata.json] 在我的项目中。它有 [null],对于 Angular 的所有当前版本需要 []。更改后,我就能获得正常的 ng build --prod 构建读数。