【问题标题】:Angular library build error: TypeError: Cannot read property 'type' of nullAngular 库构建错误:TypeError:无法读取 null 的属性“类型”
【发布时间】:2018-11-05 10:08:27
【问题描述】:

在 Angular v6 中,当我尝试构建我的库时,我遇到了一个非常不具描述性的 BUILD ERROR Cannot read property 'type' of null

BUILD ERROR
Cannot read property 'type' of null
TypeError: Cannot read property 'type' of null
    at /Users/John/apps/tests/service-work/node_modules/@angular/compiler/bundles/compiler.umd.js:15378:27
    at Array.forEach (<anonymous>)
    at removeSummaryDuplicates (/Users/John/apps/tests/service-work/node_modules/@angular/compiler/bundles/compiler.umd.js:15377:11)
    at TemplateParser.tryParseHtml (/Users/John/apps/tests/service-work/node_modules/@angular/compiler/bundles/compiler.umd.js:14806:34)
    at TemplateParser.tryParse (/Users/John/apps/tests/service-work/node_modules/@angular/compiler/bundles/compiler.umd.js:14799:21)
    at TemplateParser.parse (/Users/John/apps/tests/service-work/node_modules/@angular/compiler/bundles/compiler.umd.js:14780:27)
    at AotCompiler._parseTemplate (/Users/John/apps/tests/service-work/node_modules/@angular/compiler/bundles/compiler.umd.js:20483:43)
    at AotCompiler._createTypeCheckBlock (/Users/John/apps/tests/service-work/node_modules/@angular/compiler/bundles/compiler.umd.js:20250:23)
    at /Users/John/apps/tests/service-work/node_modules/@angular/compiler/bundles/compiler.umd.js:20220:27
    at Array.forEach (<anonymous>)

已经解决了这个问题,我发布这篇文章是为了帮助其他遇到这个问题的人。

【问题讨论】:

    标签: angular angular-aot


    【解决方案1】:

    我通过安装@angular/cdk 8.0.0 版本来解决它。

    当我安装 8.2.3 时,因为 build 会报错为Cannot read property 'type' of null

    npm i @angular/cdk@8.0.0 解决了

    【讨论】:

      【解决方案2】:

      知道这个帖子有点老了,但这可能会帮助遇到这个问题的人。

      我的 Angular 9 库包含一些 Angular Material 组件,我曾经/现在遇到同样的问题,我的库和使用我的库的应用程序都在使用 Ivy。

      添加 fullTemplateTypeCheck 并将其设置为 false 会抑制错误,但不利的一面是它可能会抑制您可能遇到的其他错误。

      https://github.com/ng-packagr/ng-packagr/issues/1087

      示例 tsconfig.lib.json

      "angularCompilerOptions": {
              "fullTemplateTypeCheck": false,
      },
      

      【讨论】:

        【解决方案3】:

        在开发使用另一个 Angular 库的 Angular 库时,我遇到了同样的问题。

        我建议先升级相关的依赖版本,看看能不能解决问题:

        // initial versions (did not work)
        "@angular-devkit/build-angular": "~0.13.0",
        "@angular-devkit/build-ng-packagr": "~0.13.0",
        "@angular/cli": "~7.3.1",
        "@angular/compiler-cli": "~7.2.0",
        
        // updated versions (works!)
        "@angular-devkit/build-angular": "~0.13.8",
        "@angular-devkit/build-ng-packagr": "~0.13.8",
        "@angular/cli": "~7.3.8",
        "@angular/compiler-cli": "~7.2.14",
        

        这对我有用。

        我怀疑 John 提出的解决方案也可以正常工作(因为我没有重新导出使用的传递库),如果您使用的是旧版本的 Angular 库开发工具,则需要此解决方案。但是,如果您只是升级 Angular lib 开发工具,则可能不需要它。

        【讨论】:

          【解决方案4】:

          我带着同样的错误信息来到这个页面,但是在开发 Angular 库时使用yarn link 解决了这个问题。

          这是因为默认库文件已在 tsconfig.json 文件中解析。所以在我的项目中 angular 生成了这个:

          // tsconfig.json
          ...
              "paths": {
                "lib-core": [
                  "dist/lib-core"
                ],
                "lib-core/*": [
                  "dist/lib-core/*"
                ],
          

          然后我在开发库包和应用包(使用库)时只运行2个终端

          1. ng build --project=lib-core --watch 在后台
          2. ng serve --project=my-app 开发我的应用程序

          所以我不需要 yarn link 并且我没有收到该错误消息!

          【讨论】:

          • 这里的主要问题是当您处理多个不能相互用作别名的库时
          【解决方案5】:

          更新

          我对这个问题的诊断似乎不正确(已经很久了,我不记得我是如何解决的了)。 Check out this issue in the Angular repo 听起来像是正确的诊断,

          原答案:

          所以经过大量调试后,我想通了:

          我的自定义库,我们称之为库 A,将另一个自定义库库 B 导入库 A 的 NgModule。库 B 从它的主要 NgModule 导出几个组件和模块。问题是,虽然库 B 从它的 NgModule 导出了组件和模块,但我未能也通过 javascript/typescript 导出这些组件和模块。解决方案是确保通过我在NgModule 中导出的打字稿导出任何组件/模块。

          示例问题代码

          import { NgModule } from '@angular/core';
          import { CommonModule } from '@angular/common';
          import { LibraryBComponent } from './library-b.component';
          
          @NgModule({
            imports: [CommonModule],
            declarations: [LibraryBComponent],
            exports: [LibraryBComponent],
          })
          export class LibraryBModule {}
          

          解决方案代码

          import { NgModule } from '@angular/core';
          import { CommonModule } from '@angular/common';
          import { LibraryBComponent } from './library-b.component';
          
          export { LibraryBComponent }      // <-- addition
          
          @NgModule({
            imports: [CommonModule],
            declarations: [LibraryBComponent],
            exports: [LibraryBComponent],
          })
          export class LibraryBModule {}
          

          【讨论】:

          • 不幸的是,我的情况完全一样,这个解决方案对我不起作用。在某种程度上这是有道理的,因为我正在导出的组件已经通过public_api.tsbucket 从它自己的文件中导出。
          • @GeorgeKnap 已经够久了,我不记得这个问题的太多细节了,但我想我最终通过创建一个新的、工作的、空白的 Angular 应用程序来解决问题,移植我的模块并逐段编写代码,并在搜索构建错误的过程中逐步重建应用程序。这样我就能够准确地弄清楚是什么代码破坏了事情。这是一个非常耗时的调试过程。
          • @GeorgeKnap 另外,我可能记错了,但我想我在从 angular v5 升级到 v6 时遇到了这个问题,我认为问题之一是我的 polyfills.ts 文件是缺少必需的 polyfill(我认为是 Reflect api,但我不确定)。
          • 我在库 B 中包含了 Angular 材料,而库 A 无法再编译。我只得到了著名的“无法读取 null 的属性 'type'” 所以解决方法是在库 B 的模块文件中导出包含的 Material 模块。 import { MatTooltipModule } from '@angular/material/tooltip';导出 { MatTooltipModule }
          【解决方案6】:

          我的问题是在&lt;projectRoot&gt;/tsconfig.json 上(你可以在#24143(comment) 上查看)

          基本上我必须从tsconfig.json 中删除路径,例如:

            "library-b": [
              "dist/library-b"
            ],
            "library-b/*": [
              "dist/library-b/*"
            ]
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2021-12-03
            • 2023-01-31
            • 2021-11-24
            • 2019-11-02
            • 2017-11-15
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多