Angular 16集成single-spa-angular启动报错:找不到指定builder包
问题
我在为Angular 16应用搭建基于single-spa的微前端架构时,执行ng add single-spa-angular安装依赖后,启动项目报错:Error: Could not find the '@angular-builders/custom-webpack:dev-server' builder's node package.
附上我的配置文件:
angular.json
{ "$schema": "./node_modules/@angular/cli/lib/config/schema.json", "version": 1, "newProjectRoot": "projects", "projects": { "app1": { "projectType": "application", "schematics": {}, "root": "", "sourceRoot": "src", "prefix": "app", "architect": { "build": { "builder": "@angular-builders/custom-webpack:browser", "options": { "outputPath": "dist/app1", "index": "src/index.html", "main": "src/main.single-spa.ts", "polyfills": [ "zone.js" ], "tsConfig": "tsconfig.app.json", "assets": [ "src/favicon.ico", "src/assets" ], "styles": [ "src/styles.css" ], "scripts": [], "customWebpackConfig": { "path": "extra-webpack.config.js", "libraryName": "app1", "libraryTarget": "umd" }, "deployUrl": "http://localhost:4200/" }, "configurations": { "production": { "budgets": [ { "type": "initial", "maximumWarning": "500kb", "maximumError": "1mb" }, { "type": "anyComponentStyle", "maximumWarning": "2kb", "maximumError": "4kb" } ], "outputHashing": "none" }, "development": { "buildOptimizer": false, "optimization": false, "vendorChunk": true, "extractLicenses": false, "sourceMap": true, "namedChunks": true, "outputHashing": "none" } }, "defaultConfiguration": "production" }, "serve": { "builder": "@angular-builders/custom-webpack:dev-server", "configurations": { "production": { "browserTarget": "app1:build:production" }, "development": { "browserTarget": "app1:build:development" } }, "defaultConfiguration": "development" }, "extract-i18n": { "builder": "@angular-devkit/build-angular:extract-i18n", "options": { "browserTarget": "app1:build" } }, "test": { "builder": "@angular-devkit/build-angular:karma", "options": { "polyfills": [ "zone.js", "zone.js/testing" ], "tsConfig": "tsconfig.spec.json", "assets": [ "src/favicon.ico", "src/assets" ], "styles": [ "src/styles.css" ], "scripts": [] } } } } } }
package.json
{ "name": "app1", "version": "0.0.0", "scripts": { "ng": "ng", "start": "ng serve", "build": "ng build", "watch": "ng build --watch --configuration development", "test": "ng test", "build:single-spa:app1": "ng build app1 --configuration production", "serve:single-spa:app1": "ng s --project app1 --disable-host-check --port 4200 --live-reload false" }, "private": true, "dependencies": { "@angular/animations": "^16.2.0", "@angular/common": "^16.2.0", "@angular/compiler": "^16.2.0", "@angular/core": "^16.2.0", "@angular/forms": "^16.2.0", "@angular/platform-browser": "^16.2.0", "@angular/platform-browser-dynamic": "^16.2.0", "@angular/router": "^16.2.0", "rxjs": "~7.8.0", "single-spa": ">=4.0.0", "single-spa-angular": "^9.0.1", "tslib": "^2.3.0", "zone.js": "~0.13.0" }, "devDependencies": { "@angular-builders/custom-webpack": "16.0.1", "@angular-devkit/build-angular": "^16.2.13", "@angular/cli": "^16.2.13", "@angular/compiler-cli": "^16.2.0", "@types/jasmine": "~4.3.0", "jasmine-core": "~4.6.0", "karma": "~6.4.0", "karma-chrome-launcher": "~3.2.0", "karma-coverage": "~2.2.0", "karma-jasmine": "~5.1.0", "karma-jasmine-html-reporter": "~2.1.0", "style-loader": "^3.3.1", "typescript": "~5.1.3" } }
解决方案
1. 重新安装依赖
先清理现有依赖和锁文件,重新安装确保完整性:
rm -rf node_modules package-lock.json npm install
2. 替换serve的builder配置
将angular.json中architect.serve的builder替换为Angular官方的dev-server,保留custom-webpack的build配置即可:
"serve": { "builder": "@angular-devkit/build-angular:dev-server", "configurations": { "production": { "browserTarget": "app1:build:production" }, "development": { "browserTarget": "app1:build:development" } }, "defaultConfiguration": "development" }
3. 升级single-spa-angular到兼容版本
当前使用的single-spa-angular@9.0.1与Angular 16版本不匹配,升级到对应兼容版本:
npm install single-spa-angular@^16.0.0 --save
4. 使用专用启动命令
启动项目时,改用package.json中配置的single-spa专用启动命令:
npm run serve:single-spa:app1
内容的提问来源于stack exchange,提问作者Subham
相关产品推荐
相关产品推荐

