Angular 14安装ngx-bootstrap报不支持schematics、手动安装报错问题
Angular 14 安装 ngx-bootstrap 异常解决
问题描述
项目基于Angular 14搭建,package.json依赖配置如下:
{ "name": "testproj", "version": "0.0.0", "scripts": { "ng": "ng", "start": "ng serve", "build": "ng build", "watch": "ng build --watch --configuration development", "test": "ng test" }, "private": true, "dependencies": { "@angular/animations": "^14.0.0", "@angular/common": "^14.0.0", "@angular/compiler": "^14.0.0", "@angular/core": "^14.0.0", "@angular/forms": "^14.0.0", "@angular/platform-browser": "^14.0.0", "@angular/platform-browser-dynamic": "^14.0.0", "@angular/router": "^14.0.0", "rxjs": "~7.5.0", "tslib": "^2.3.0", "zone.js": "~0.11.4" }, "devDependencies": { "@angular-devkit/build-angular": "^14.0.3", "@angular/cli": "~14.0.3", "@angular/compiler-cli": "^14.0.0", "@types/jasmine": "~4.0.0", "jasmine-core": "~4.1.0", "karma": "~6.3.0", "karma-chrome-launcher": "~3.1.0", "karma-coverage": "~2.2.0", "karma-jasmine": "~5.0.0", "karma-jasmine-html-reporter": "~1.7.0", "typescript": "~4.7.2" } }
执行ng add ngx-bootstrap命令安装时抛出错误:
Package "ngx-bootstrap" was found but does not support schematics
后续尝试手动执行npm安装命令,依然触发依赖冲突报错,无法完成安装。
问题根因
ng add命令默认拉取最新版ngx-bootstrap,10.0.0及以上版本的ngx-bootstrap最低要求Angular 15+版本,不再为Angular 14提供schematics安装支持,因此触发不支持schematics的报错。- 最新版ngx-bootstrap的peer依赖要求和当前项目Angular 14版本不匹配,手动安装时会触发依赖校验失败,导致安装中断。
解决步骤
- 清理本地安装缓存,避免旧缓存或残留依赖干扰
执行以下命令:
Windows环境可直接在资源管理器删除# 清理npm缓存 npm cache clean --force # 删除现有依赖目录和锁文件 rm -rf node_modules package-lock.jsonnode_modules文件夹和package-lock.json文件,或在命令行执行:rd /s /q node_modules del package-lock.json - 安装Angular 14兼容的ngx-bootstrap版本,禁止直接安装最新版,Angular 14对应兼容版本为9.0.0:
npm install ngx-bootstrap@9.0.0 --save - 安装ngx-bootstrap依赖的bootstrap样式库,选择和Angular 14兼容的5.x版本即可:
npm install bootstrap@5.2.3 --save - 配置全局样式引用,打开项目根目录下的
angular.json文件,找到build.options.styles配置项,添加bootstrap样式路径:"styles": [ "node_modules/bootstrap/dist/css/bootstrap.min.css", "src/styles.css" ] - 按需在业务模块中导入需要使用的ngx-bootstrap功能模块即可,例如下拉、模态框模块导入示例:
// app.module.ts import { NgModule } from '@angular/core'; import { BrowserModule } from '@angular/platform-browser'; import { BsDropdownModule } from 'ngx-bootstrap/dropdown'; import { ModalModule } from 'ngx-bootstrap/modal'; import { AppComponent } from './app.component'; @NgModule({ declarations: [AppComponent], imports: [ BrowserModule, // 导入需要的功能模块 BsDropdownModule.forRoot(), ModalModule.forRoot() ], bootstrap: [AppComponent] }) export class AppModule { } - 执行
npm install重新全量安装依赖,之后执行ng serve启动项目验证即可。
内容的提问来源于stack exchange,提问作者Angular Dev
相关产品推荐
相关产品推荐

