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

# bun upgrade

> 将依赖项更新到最新版本

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

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

```sh terminal icon="terminal" theme={"theme":{"light":"github-light","dark":"dracula"}}
bun update
```

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

```sh terminal icon="terminal" theme={"theme":{"light":"github-light","dark":"dracula"}}
bun update [package]
```

## `--interactive`

为了获得更可控的更新体验，使用 `--interactive` 标志选择要更新的包：

```sh terminal icon="terminal" theme={"theme":{"light":"github-light","dark":"dracula"}}
bun update --interactive
bun update -i
```

这将启动一个交互式终端界面，显示所有过期的包及其当前版本和目标版本。然后你可以选择要更新的包。

### 交互界面

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

```txt theme={"theme":{"light":"github-light","dark":"dracula"}}
? Select packages to update - Space to toggle, Enter to confirm, a to select all, n to select none, i to invert, l to toggle latest

  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`

配合 `--interactive` 使用 `--recursive` 标志可以在 monorepo 的所有工作区内更新依赖：

```sh terminal icon="terminal" theme={"theme":{"light":"github-light","dark":"dracula"}}
bun update --interactive --recursive
bun update -i -r
```

该命令会额外显示一个 “Workspace” 列，显示每个依赖所属的工作区。

## `--latest`

默认情况下，`bun update` 会更新到符合 `package.json` 中版本范围的最新依赖版本。

如果想更新到不受当前版本范围限制的最新版本，可以使用 `--latest` 标志：

```sh terminal icon="terminal" theme={"theme":{"light":"github-light","dark":"dracula"}}
bun update --latest
```

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

例如，以下 `package.json`：

```json package.json icon="file-json" theme={"theme":{"light":"github-light","dark":"dracula"}}
{
  "dependencies": {
    "react": "^17.0.2"
  }
}
```

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

***

## CLI 用法

```bash terminal icon="terminal" theme={"theme":{"light":"github-light","dark":"dracula"}}
bun update <package> <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> 或保存锁文件
</ParamField>

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

<ParamField path="--frozen-lockfile" type="boolean">
  禁止修改锁文件
</ParamField>

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

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

### 网络与注册表

<ParamField path="--ca" type="string">
  提供证书颁发机构（CA）签发证书
</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" default="5">
  生命周期脚本的最大并发作业数（默认 5）
</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">
  设置特定的当前工作目录
</ParamField>

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