> ## Documentation Index
> Fetch the complete documentation index at: https://bun.ll1025.cn/llms.txt
> Use this file to discover all available pages before exploring further.

# Overrides 和 Resolutions

> 使用 npm overrides 和 Yarn resolutions 控制元依赖版本

Bun 支持 `package.json` 中的 npm `"overrides"` 和 Yarn `"resolutions"`。两者都用于指定\_元依赖\_（即你依赖的依赖）的版本范围。

```json package.json icon="file-json" theme={null}
{
  "name": "my-app",
  "dependencies": {
    "foo": "^2.0.0"
  },
  "overrides": { // [!code ++]
    "bar": "~4.4.0" // [!code ++]
  } // [!code ++]
}
```

默认情况下，Bun 会根据每个包 `package.json` 中指定的范围安装所有依赖和元依赖的最新版本。假设你的项目有一个依赖 `foo`，而 `foo` 又依赖于 `bar`。这使得 `bar` 成为你项目的一个\_元依赖\_。

```json package.json icon="file-json" theme={null}
{
  "name": "my-app",
  "dependencies": {
    "foo": "^2.0.0"
  }
}
```

当你运行 `bun install` 时，Bun 会安装每个包的最新版本。

```txt tree layout of node_modules icon="list-tree" theme={null}
node_modules
├── foo@1.2.3
└── bar@4.5.6
```

如果在 `bar@4.5.6` 中引入了安全漏洞，你可能希望将 `bar` 固定到没有该漏洞的旧版本。这就是 `"overrides"` 和 `"resolutions"` 的作用。

***

## \`"overrides""

将 `bar` 添加到 `package.json` 的 `"overrides"` 字段中。Bun 在确定要安装的 `bar` 版本时（无论是依赖还是元依赖），会优先使用指定的版本范围。

<Note>
  Bun 只支持顶层的 `"overrides"`，不支持[嵌套 overrides](https://docs.npmjs.com/cli/v9/configuring-npm/package-json#overrides)。
</Note>

```json package.json icon="file-json" theme={null}
{
  "name": "my-app",
  "dependencies": {
    "foo": "^2.0.0"
  },
  "overrides": { // [!code ++]
    "bar": "~4.4.0" // [!code ++]
  } // [!code ++]
}
```

## `"resolutions"`

`"resolutions"` 是 Yarn 对 `"overrides"` 的替代方案，语法类似。Bun 支持它以便更轻松地从 Yarn 迁移。

与 `"overrides"` 一样，不支持\_嵌套 resolutions\_。

```json package.json icon="file-json" theme={null}
{
  "name": "my-app",
  "dependencies": {
    "foo": "^2.0.0"
  },
  "resolutions": { // [!code ++]
    "bar": "~4.4.0" // [!code ++]
  } // [!code ++]
}
```
