Angular 8项目执行ng add @angular/bazel报Schematic 'ng-add'未找到错误
The Problem
When running ng add @angular/bazel in an Angular 8 project, you'll hit two sequential errors:
- First, a warning:
Error Unable to find compatible package. Using 'latest' - After dependencies finish installing, an unhandled exception:
An unhandled exception occurred: Schematic "ng-add" not found in collection "@angular/bazel"
Here's the full error stack trace for reference:
[error] Error: Schematic "ng-add" not found in collection "@angular/bazel". at SchematicEngine.createSchematic (/home/flolu/Desktop/cents-ideas/services/frontend/node_modules/@angular/cli/node_modules/@angular-devkit/schematics/src/engine/engine.js:232:23) at CollectionImpl.createSchematic (/home/flolu/Desktop/cents-ideas/services/frontend/node_modules/@angular/cli/node_modules/@angular-devkit/schematics/src/engine/engine.js:69:29) at AddCommand.getSchematic (/home/flolu/Desktop/cents-ideas/services/frontend/node_modules/@angular/cli/models/schematic-command.js:135:27) at AddCommand.runSchematic (/home/flolu/Desktop/cents-ideas/services/frontend/node_modules/@angular/cli/models/schematic-command.js:291:32) at AddCommand.executeSchematic (/home/flolu/Desktop/cents-ideas/services/frontend/node_modules/@angular/cli/commands/add-impl.js:148:31) at AddCommand.run (/home/flolu/Desktop/cents-ideas/services/frontend/node_modules/@angular/cli/commands/add-impl.js:45:25) at AddCommand.validateAndRun (/home/flolu/Desktop/cents-ideas/services/frontend/node_modules/@angular/cli/models/command.js:134:39) at process._tickCallback (internal/process/next_tick.js:68:7) at Function.Module.runMain (internal/modules/cjs/loader.js:745:11) at startup (internal/bootstrap/node.js:283:19) at bootstrapNodeJSCore (internal/bootstrap/node.js:743:3)
This issue is reproducible by running ng add @angular/bazel in the /services/frontend project directory.
Why This Happens
The root cause is straightforward: the latest version of @angular/bazel no longer supports Angular 8, and newer releases have removed the ng-add schematic that the Angular CLI depends on for the ng add command. When the CLI can't locate a version compatible with Angular 8, it automatically pulls the latest package—which lacks the required schematic, triggering the error.
Solutions
1. Install a Version of @angular/bazel Compatible with Angular 8
You need to pin the package to an 8.x release that works with Angular 8. Follow these steps:
First, clean up any existing incompatible installations:
# For Yarn yarn remove @angular/bazel # For npm npm uninstall @angular/bazel
Then install the compatible version (use the latest patch for Angular 8, which is 8.3.29):
# Yarn yarn add @angular/bazel@8.3.29 --dev # npm npm install @angular/bazel@8.3.29 --save-dev
2. Manually Set Up Bazel Integration
Note that Angular 8's @angular/bazel doesn't include an ng-add schematic, so you'll need to configure it manually. Here's what you'll need to do:
- Add Bazel-specific build targets to your
angular.json - Create and configure
WORKSPACEandBUILD.bazelfiles in your project root - Update
tsconfig.jsonto meet Bazel's compilation requirements
You can refer to Angular 8-era Bazel documentation for detailed steps on these configurations.
3. Upgrade Your Angular Project (Recommended)
If your project allows it, upgrading to Angular 10 or later (preferably Angular 12+) is a better long-term fix. Newer Angular versions have full, polished support for @angular/bazel, and the ng add command will work as expected without manual workarounds. Follow official Angular upgrade guides to step through version updates safely.
Verify the Fix
After completing the setup, run ng build or bazel build to confirm that Bazel is integrated correctly with your Angular 8 project.
内容的提问来源于stack exchange,提问作者Florian Ludewig

