> ## 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.

# bun update

> 将依赖更新到最新版本

<Note>要升级你的 Bun CLI 版本，请参阅 [`bun upgrade`](/installation#upgrading)。</Note>

要将所有依赖更新到最新版本：

```sh terminal icon="terminal" theme={null}
bun update
```

将特定依赖更新到最新版本：

```sh terminal icon="terminal" theme={null}
bun update [package]
```

## `--interactive`

使用 `--interactive` 标志选择要更新的包：

```sh terminal icon="terminal" theme={null}
bun update --interactive
bun update -i
```

该标志会打开一个终端界面，列出每个过期包及其当前版本和目标版本。

### 交互界面

界面按依赖类型分组显示包：

```txt theme={null}
? 选择要更新的包 - 空格键切换，回车确认，a 全选，n 全不选，i 反选，l 切换最新版本

  dependencies                Current  Target   Latest
    □ react                   17.0.2   18.2.0   18.3.1
    □ lodash                  4.17.20  4.17.21  4.17.21

  devDependencies             Current  Target   Latest
    □ typescript              4.8.0    5.0.0    5.3.3
    □ @types/node             16.11.7  18.0.0   20.11.5

  optionalDependencies        Current  Target   Latest
    □ some-optional-package   1.0.0    1.1.0    1.2.0
```

**分区：**

* 包按分区标题分组：`dependencies`、`devDependencies`、`peerDependencies`、`optionalDependencies`
* 每个分区显示与包数据对齐的列标题

**列：**

* **Package（包）**：包名（可能有后缀如 ` dev`、` peer` 或 ` optional`）
* **Current（当前）**：当前安装的版本
* **Target（目标）**：将安装的版本（遵循 semver 约束）
* **Latest（最新）**：可用的最新版本

### 键盘控制

**选择：**

* **空格键**：切换包的选择状态
* **回车键**：确认选择并更新
* **a/A**：选择所有包
* **n/N**：全不选择
* **i/I**：反选

**导航：**

* **↑/↓ 方向键** 或 **j/k**：移动光标
* **l/L**：切换当前包的目标版本和最新版本

**退出：**

* **Ctrl+C** 或 **Ctrl+D**：取消而不更新

### 视觉指示器

* **■** 已选择的包（将被更新）
* **□** 未选择的包
* **❯** 当前光标位置
* **颜色**：红色（主版本）、黄色（次版本）、绿色（补丁版）版本变更
* **下划线**：当前选择的更新目标

### 包分组

包按依赖类型分区组织：

* **dependencies** - 常规运行时依赖
* **devDependencies** - 开发依赖
* **peerDependencies** - 对等依赖
* **optionalDependencies** - 可选依赖

在每个分区中，单个包可能有后缀（` dev`、` peer`、` optional`）。

## `--recursive`

使用 `--recursive` 标志与 `--interactive` 一起，可以跨单体仓库中的所有工作空间更新依赖：

```sh terminal icon="terminal" theme={null}
bun update --interactive --recursive
bun update -i -r
```

使用 `--recursive` 时，界面会添加一个"Workspace"列，显示每个依赖所属的工作空间。

## `--latest`

默认情况下，`bun update` 将每个依赖更新到满足 `package.json` 中版本范围的最新版本。

要更新到最新版本（无论是否满足该范围），请使用 `--latest` 标志：

```sh terminal icon="terminal" theme={null}
bun update --latest
```

在交互模式下，按 **l** 键可以切换包的目标版本（遵循 semver）和最新版本。

例如，对于以下 `package.json`：

```json package.json icon="file-json" theme={null}
{
  "dependencies": {
    "react": "^17.0.2"
  }
}
```

* `bun update` 会更新到匹配 `17.x` 的版本。
* `bun update --latest` 会更新到匹配 `18.x` 或更高版本的版本。

***

## CLI 用法

```bash terminal icon="terminal" theme={null}
bun update <name>@<version>
```

### 更新策略

<ParamField path="--force" type="boolean">
  始终从注册表请求最新版本并重新安装所有依赖。别名：<code>-f</code>
</ParamField>

<ParamField path="--latest" type="boolean">
  将包更新到最新版本
</ParamField>

### 依赖范围

<ParamField path="--production" type="boolean">
  不安装 devDependencies。别名：<code>-p</code>
</ParamField>

<ParamField path="--global" type="boolean">
  全局安装。别名：<code>-g</code>
</ParamField>

<ParamField path="--omit" type="string">
  从安装中排除 <code>dev</code>、<code>optional</code> 或 <code>peer</code> 依赖
</ParamField>

### 项目文件管理

<ParamField path="--yarn" type="boolean">
  写入 <code>yarn.lock</code> 文件（yarn v1）。别名：<code>-y</code>
</ParamField>

<ParamField path="--no-save" type="boolean">
  不更新 <code>package.json</code> 或不保存 lockfile
</ParamField>

<ParamField path="--save" type="boolean" default="true">
  保存到 <code>package.json</code>（默认为 true）
</ParamField>

<ParamField path="--frozen-lockfile" type="boolean">
  禁止更改 lockfile
</ParamField>

<ParamField path="--save-text-lockfile" type="boolean">
  保存基于文本的 lockfile
</ParamField>

<ParamField path="--lockfile-only" type="boolean">
  仅生成 lockfile，不安装依赖
</ParamField>

### 网络与注册表

<ParamField path="--ca" type="string">
  提供证书颁发机构签名证书
</ParamField>

<ParamField path="--cafile" type="string">
  与 <code>--ca</code> 相同，但指定证书的文件路径
</ParamField>

<ParamField path="--registry" type="string">
  使用特定注册表，覆盖 <code>.npmrc</code>、<code>bunfig.toml</code> 和环境变量
</ParamField>

<ParamField path="--network-concurrency" type="number" default="48">
  最大并发网络请求数（默认 48）
</ParamField>

### 缓存

<ParamField path="--cache-dir" type="string">
  从特定目录路径存储和加载缓存数据
</ParamField>

<ParamField path="--no-cache" type="boolean">
  完全忽略清单缓存
</ParamField>

### 输出与日志

<ParamField path="--silent" type="boolean">
  不输出任何日志
</ParamField>

<ParamField path="--verbose" type="boolean">
  极度详细的日志输出
</ParamField>

<ParamField path="--no-progress" type="boolean">
  禁用进度条
</ParamField>

<ParamField path="--no-summary" type="boolean">
  不打印摘要
</ParamField>

### 脚本执行

<ParamField path="--ignore-scripts" type="boolean">
  跳过项目 <code>package.json</code> 中的生命周期脚本（依赖的脚本从不运行）
</ParamField>

<ParamField path="--concurrent-scripts" type="number">
  生命周期脚本的最大并发作业数（默认：2 倍 CPU 核心数）
</ParamField>

### 安装控制

<ParamField path="--no-verify" type="boolean">
  跳过验证新下载包的完整性
</ParamField>

<ParamField path="--trust" type="boolean">
  添加到项目 <code>package.json</code> 的 <code>trustedDependencies</code> 中并安装该包
</ParamField>

<ParamField path="--backend" type="string" default="clonefile">
  安装依赖的特定平台优化。可能的值：<code>clonefile</code>（默认）、<code>hardlink</code>、<code>symlink</code>、<code>copyfile</code>
</ParamField>

### 通用与环境

<ParamField path="--config" type="string">
  指定配置文件路径（<code>bunfig.toml</code>）。别名：<code>-c</code>
</ParamField>

<ParamField path="--dry-run" type="boolean">
  不实际安装任何内容
</ParamField>

<ParamField path="--cwd" type="string">
  设置特定的 cwd
</ParamField>

<ParamField path="--help" type="boolean">
  打印此帮助菜单。别名：<code>-h</code>
</ParamField>
