You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

执行ng build --prod时ng2-pdf-viewer构建错误原因咨询

Fixing "Cannot read property 'kind' of undefined" in ng build --prod with ng2-pdf-viewer

Hey there, let's tackle this frustrating error you're hitting when running ng build --prod with the ng2-pdf-viewer library. This issue almost always stems from version mismatches between your Angular setup and the library, or conflicts with Angular's Build Optimizer tool. Here are the most reliable fixes to try:

1. Match ng2-pdf-viewer to your Angular version

First up, double-check that you're using a version of ng2-pdf-viewer compatible with your Angular project. Different Angular releases require specific library versions to play nice.

  • Uninstall your current version first:
    npm uninstall ng2-pdf-viewer
    
  • Then install a version aligned with your Angular version:
    • Angular 12+: Use ng2-pdf-viewer@9.x.x or newer
    • Angular 10-11: Go for ng2-pdf-viewer@8.x.x
    • Angular 8-9: Stick with ng2-pdf-viewer@7.x.x

You can confirm exact compatibility by checking the library's release notes (look for version-specific Angular support mentions).

2. Disable Build Optimizer temporarily

If you need a quick fix without changing library versions, you can turn off Angular's Build Optimizer—the component throwing the error. Here's how:

  1. Open your project's angular.json file
  2. Locate the configurations.production section
  3. Set buildOptimizer to false:
    "configurations": {
      "production": {
        "buildOptimizer": false,
        "optimization": true,
        "outputHashing": "all",
        // keep your other existing settings intact
      }
    }
    
  4. Save the file and re-run ng build --prod

Note: This will make your final bundle slightly larger, but it's a valid temporary workaround while you sort out version compatibility.

3. Update your Angular Devkit packages

Outdated versions of @angular-devkit/build-angular (which includes Build Optimizer) can have bugs that trigger this error. Try updating these packages:

npm update @angular-devkit/build-angular --save-dev

If your global Angular CLI is also outdated, update it too:

npm install -g @angular/cli@latest

Why this error happens

The error occurs because Angular's Build Optimizer tries to process ng2-pdf-viewer's code but hits a missing kind property in some decorator metadata. This usually means the library's code isn't formatted in a way the current Build Optimizer expects—almost always due to a version mismatch between the library, Angular, and Devkit tools.

内容的提问来源于stack exchange,提问作者mohame mostafa

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.06 21:42:52