执行ng build --prod时ng2-pdf-viewer构建错误原因咨询
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.xor newer - Angular 10-11: Go for
ng2-pdf-viewer@8.x.x - Angular 8-9: Stick with
ng2-pdf-viewer@7.x.x
- Angular 12+: Use
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:
- Open your project's
angular.jsonfile - Locate the
configurations.productionsection - Set
buildOptimizertofalse:"configurations": { "production": { "buildOptimizer": false, "optimization": true, "outputHashing": "all", // keep your other existing settings intact } } - 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

