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

Angular 6库构建报错:@ng-bootstrap/ng-bootstrap需显式白名单

我之前遇到过完全一样的问题,这是Angular CLI构建库时的常见限制——它默认会严格管控外部依赖,避免意外打包冗余代码或引发版本冲突。下面是我亲测有效的解决步骤:

解决步骤

1. 修改库的ng-package.json配置

找到你的库目录(通常是projects/@some/libName/)下的ng-package.json文件,添加whitelistedNonPeerDependencies字段,将@ng-bootstrap/ng-bootstrap加入白名单:

{
  "$schema": "../../node_modules/ng-packagr/ng-package.schema.json",
  "dest": "../../dist/@some/libName",
  "lib": {
    "entryFile": "src/public-api.ts",
    "whitelistedNonPeerDependencies": [
      "@ng-bootstrap/ng-bootstrap"
    ]
  }
}

这个配置会告诉Angular库构建工具ng-packagr:允许该依赖被纳入构建流程,无需强制标记为peerDependency。

2. 确认依赖的安装配置

确保@ng-bootstrap/ng-bootstrap被正确添加为库的dependencies(而非peerDependencies)——因为你的组件直接依赖它的功能。如果之前是在根目录安装的,记得在库的package.json中补充:

"dependencies": {
  "@ng-bootstrap/ng-bootstrap": "^x.x.x" // 替换为你实际安装的版本号
}

3. 清理缓存后重新构建

有时候缓存会导致配置不生效,先清理缓存再执行构建命令:

ng clean
ng build @some/libName --prod
额外说明

如果你的设计意图是让使用该库的项目自行提供@ng-bootstrap/ng-bootstrap(避免重复打包),那可以将其设为peerDependencies,此时就不需要白名单,但一定要在库的README中明确告知用户需要自行安装该依赖。不过对于组件直接依赖的场景,加入白名单并作为dependencies是最直接的解决方式。

内容的提问来源于stack exchange,提问作者Aniruddha Das

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.27 07:29:21