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

> 解释为什么安装了某个包

`bun why` 命令通过显示导致某个包被安装的依赖链，来解释该包为何会被安装在你的项目中。

## 用法

```bash terminal icon="terminal" theme={"theme":{"light":"github-light","dark":"dracula"}}
bun why <package>
```

## 参数

* `<package>`：需要解释的包名。支持类似 `@org/*` 或 `*-lodash` 的通配符模式。

## 选项

* `--top`：只显示顶层依赖，而不是完整的依赖树。
* `--depth <number>`：显示依赖树的最大层级深度。

## 示例

检查某个特定包为何被安装：

```bash terminal icon="terminal" theme={"theme":{"light":"github-light","dark":"dracula"}}
bun why react
```

```txt theme={"theme":{"light":"github-light","dark":"dracula"}}
react@18.2.0
  └─ my-app@1.0.0 (requires ^18.0.0)
```

检查所有符合特定模式的包为何被安装：

```bash terminal icon="terminal" theme={"theme":{"light":"github-light","dark":"dracula"}}
bun why "@types/*"
```

```txt theme={"theme":{"light":"github-light","dark":"dracula"}}
@types/react@18.2.15
  └─ dev my-app@1.0.0 (requires ^18.0.0)

@types/react-dom@18.2.7
  └─ dev my-app@1.0.0 (requires ^18.0.0)
```

只显示顶层依赖：

```bash terminal icon="terminal" theme={"theme":{"light":"github-light","dark":"dracula"}}
bun why express --top
```

```txt theme={"theme":{"light":"github-light","dark":"dracula"}}
express@4.18.2
  └─ my-app@1.0.0 (requires ^4.18.2)
```

限制依赖树的显示深度：

```bash terminal icon="terminal" theme={"theme":{"light":"github-light","dark":"dracula"}}
bun why express --depth 2
```

```txt theme={"theme":{"light":"github-light","dark":"dracula"}}
express@4.18.2
  └─ express-pollyfill@1.20.1 (requires ^4.18.2)
     └─ body-parser@1.20.1 (requires ^1.20.1)
     └─ accepts@1.3.8 (requires ^1.3.8)
        └─ (更深的依赖被隐藏)
```

## 理解输出内容

输出显示：

* 被查询的包名和版本
* 导致该包安装的依赖链
* 依赖的类型（开发依赖 dev、同行依赖 peer、可选依赖 optional 或生产依赖 production）
* 每个包依赖中指定的版本要求

对于嵌套依赖，命令默认显示完整的依赖树，缩进表示层级关系。
