Skip to main content
Bun 支持 [npm] 的 "overrides" 和 Yarn 的 "resolutions" 在 [package.json] 中。这些是为_元依赖_指定版本范围的机制——你的依赖的依赖。
package.json
{
  "name": "my-app",
  "dependencies": {
    "foo": "^2.0.0"
  },
  "overrides": { 
    "bar": "~4.4.0"
  } 
}
默认情况下,Bun 将根据每个包的 [package.json] 中指定的范围安装所有依赖和元依赖的最新版本。假设你有一个项目,它有一个依赖 foo,而 foo 又依赖于 bar。这意味着 bar 是我们项目的_元依赖_。
package.json
{
  "name": "my-app",
  "dependencies": {
    "foo": "^2.0.0"
  }
}
当你运行 bun install 时,Bun 将安装每个包的最新版本。
tree layout of node_modules
node_modules
├── foo@1.2.3
└── bar@4.5.6
但如果 bar@4.5.6 中引入了安全漏洞怎么办?我们可能希望将 bar 固定到没有漏洞的旧版本。这就是 "overrides"/"resolutions" 的用武之地。

"overrides"

在 [package.json] 的 "overrides" 字段中添加 bar。当确定要安装哪个版本的 bar 时,无论是依赖还是元依赖,Bun 都会遵循指定的版本范围。
Bun 目前仅支持顶层 "overrides"嵌套 覆盖 不受支持。
package.json
{
  "name": "my-app",
  "dependencies": {
    "foo": "^2.0.0"
  },
  "overrides": { 
    "bar": "~4.4.0"
  } 
}

"resolutions"

语法与 "resolutions" 类似,这是 Yarn 对 "overrides" 的替代方案。Bun 支持此功能是为了让从 Yarn 迁移更容易。 "overrides" 一样,_嵌套解析_目前不受支持。
package.json
{
  "name": "my-app",
  "dependencies": {
    "foo": "^2.0.0"
  },
  "resolutions": { 
    "bar": "~4.4.0"
  } 
}