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

在Angular项目中添加Bootstrap时出现npm依赖冲突错误求助

Angular依赖冲突解决:执行ng add @ng-bootstrap/ng-bootstrap时出现ERESOLVE错误

问题重现

执行命令ng add @ng-bootstrap/ng-bootstrap集成Bootstrap时,npm抛出依赖解析错误,核心冲突为:项目当前使用@angular/compiler@15.0.4,但@angular/localize@^15.0.0的peer依赖要求@angular/compiler@15.1.0,导致包安装及Schematic流程失败。完整报错信息如下:

npm ERR! ERESOLVE could not resolve
npm ERR!
npm ERR! While resolving: paxium-contacts@0.0.0
npm ERR! Found: @angular/compiler@15.0.4
npm ERR! node_modules/@angular/compiler
npm ERR!   @angular/compiler@"^15.0.0" from the root project
npm ERR!   peer @angular/compiler@"15.0.4" from @angular/compiler-cli@15.0.4
npm ERR!   node_modules/@angular/compiler-cli
npm ERR!     dev @angular/compiler-cli@"^15.0.0" from the root project
npm ERR!     peer @angular/compiler-cli@"^15.0.0" from @angular-devkit/build-angular@15.0.5
npm ERR!     node_modules/@angular-devkit/build-angular
npm ERR!       dev @angular-devkit/build-angular@"^15.0.4" from the root project
npm ERR!     1 more (@ngtools/webpack)
npm ERR!   1 more (@angular/platform-browser-dynamic)
npm ERR!
npm ERR! Could not resolve dependency:
npm ERR! dev @angular/localize@"^15.0.0" from the root project
npm ERR!
npm ERR! Conflicting peer dependency: @angular/compiler@15.1.0
npm ERR! node_modules/@angular/compiler
npm ERR!   peer @angular/compiler@"15.1.0" from @angular/localize@15.1.0
npm ERR!   node_modules/@angular/localize
npm ERR!     dev @angular/localize@"^15.0.0" from the root project
npm ERR!
npm ERR! Fix the upstream dependency conflict, or retry
npm ERR! this command with --force, or --legacy-peer-deps
npm ERR! to accept an incorrect (and potentially broken) dependency resolution.

✖ Package install failed, see above.
The Schematic workflow failed. See above.

解决方案

以下是三种可行的解决方式,按推荐优先级排序:

1. 统一升级Angular包到匹配版本(推荐)

Angular生态要求核心包版本保持一致,直接升级所有@angular相关包到15.1.x版本,让依赖版本完全匹配:

ng update @angular/core@15.1 @angular/cli@15.1

该命令会自动升级所有关联的Angular核心包(包括@angular/compiler、@angular/localize等),确保版本统一,彻底解决依赖冲突。

2. 使用--legacy-peer-deps参数绕过冲突

如果暂时不需要升级Angular版本,可通过npm的--legacy-peer-deps参数忽略peer依赖检查,继续安装ng-bootstrap:

ng add @ng-bootstrap/ng-bootstrap --legacy-peer-deps

或直接用npm安装:

npm install @ng-bootstrap/ng-bootstrap --legacy-peer-deps

注意:此方法仅临时绕过冲突,可能存在潜在兼容性问题,建议仅用于测试场景。

3. 锁定@angular/localize版本到15.0.4

修改项目根目录下的package.json,将@angular/localize的版本从^15.0.0改为~15.0.4,强制安装与当前compiler版本匹配的localize:

{
  "devDependencies": {
    "@angular/localize": "~15.0.4"
  }
}

修改后执行:

npm install

完成后重新运行ng add @ng-bootstrap/ng-bootstrap即可正常安装。

内容的提问来源于stack exchange,提问作者Dave Amour

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.04 22:35:28