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

# bunx

> 运行 npm 上的包

<Note>`bunx` 是 `bun x` 的别名。安装 `bun` 时会自动安装 `bunx` CLI。</Note>

使用 `bunx` 来自动安装并运行 `npm` 上的包。它是 Bun 对 `npx` 或 `yarn dlx` 的替代方案。

```bash terminal icon="terminal" theme={null}
bunx cowsay "Hello world!"
```

<Note>
  ⚡️ **速度** — 凭借 Bun 的快速启动时间，对于本地安装的包，`bunx` [大约比 `npx` 快 100 倍](https://twitter.com/jarredsumner/status/1606163655527059458)。
</Note>

包可以在其 `package.json` 的 `"bin"` 字段中声明可执行文件。这些被称为 *包可执行文件* 或 *包二进制文件*。

```json package.json icon="file-json" theme={null}
{
  // ... 其他字段
  "name": "my-cli",
  "bin": {
    "my-cli": "dist/index.js"
  }
}
```

这些可执行文件通常是普通的 JavaScript 文件，带有 [shebang 行](https://en.wikipedia.org/wiki/Shebang_\(Unix\)) 来指定运行它们的程序。以下文件使用 `node` 运行。

```js dist/index.js icon="https://mintcdn.com/span-inc/82N53aP7NFbaCVSl/icons/javascript.svg?fit=max&auto=format&n=82N53aP7NFbaCVSl&q=85&s=48bd3119866790424aa113cd09edda91" theme={null}
#!/usr/bin/env node

console.log("Hello world!");
```

使用 `bunx` 运行这些可执行文件：

```bash terminal icon="terminal" theme={null}
bunx my-cli
```

与 `npx` 类似，`bunx` 首先检查本地是否已安装该包，如果没有，则从 `npm` 自动安装。安装的包会存储在 Bun 的[全局缓存](/pm/global-cache)中以供将来使用。

## 参数和标志

要将额外的命令行标志和参数传递给可执行文件，请将它们放在可执行文件名称之后。

```bash terminal icon="terminal" theme={null}
bunx my-cli --foo bar
```

***

## Shebangs

默认情况下，Bun 遵循 shebang。如果可执行文件标记有 `#!/usr/bin/env node`，Bun 会启动一个 `node` 进程来执行该文件。要使用 Bun 运行时来运行可执行文件，请传递 `--bun` 标志。

```bash terminal icon="terminal" theme={null}
bunx --bun my-cli
```

`--bun` 标志必须出现在可执行文件名称\_之前\_。出现在名称\_之后\_的标志会传递给可执行文件。

```bash terminal icon="terminal" theme={null}
bunx --bun my-cli # 正确
bunx my-cli --bun # 错误
```

## 包标志

**`--package <pkg>` 或 `-p <pkg>`** - 从特定包运行二进制文件。当二进制文件名与包名不同时很有用：

```bash terminal icon="terminal" theme={null}
bunx -p renovate renovate-config-validator
bunx --package @angular/cli ng
```

要强制脚本始终使用 Bun 运行，请为其指定 `bun` shebang。

```js dist/index.js icon="https://mintcdn.com/span-inc/82N53aP7NFbaCVSl/icons/javascript.svg?fit=max&auto=format&n=82N53aP7NFbaCVSl&q=85&s=48bd3119866790424aa113cd09edda91" theme={null}
#!/usr/bin/env bun
```

***

## 用法

```bash theme={null}
bunx [flags] <package>[@version] [flags and arguments for the package]
```

执行 npm 包的可执行文件（CLI）。如果该包未安装在 `node_modules` 中，Bun 会将其安装到全局共享缓存中。

### 标志

<ParamField path="--bun" type="boolean">
  强制命令使用 Bun 而非 Node.js 运行，即使可执行文件包含 Node shebang（`#!/usr/bin/env node`）
</ParamField>

<ParamField path="-p, --package" type="string">
  当二进制名称与包名称不同时指定要安装的包
</ParamField>

<ParamField path="--no-install" type="boolean">
  如果包尚未安装，跳过安装
</ParamField>

<ParamField path="--verbose" type="boolean">
  安装期间启用详细输出
</ParamField>

<ParamField path="--silent" type="boolean">
  安装期间禁止输出
</ParamField>

### 示例

```bash terminal icon="terminal" theme={null}
# 运行 Prisma 迁移
bunx prisma migrate

# 使用 Prettier 格式化文件
bunx prettier foo.js

# 运行特定版本的包
bunx uglify-js@3.14.0 app.js

# 当二进制名称与包名称不同时使用 --package
bunx -p @angular/cli ng new my-app

# 强制使用 Bun 而非 Node.js 运行，即使可执行文件包含 Node shebang
bunx --bun vite dev foo.js
```
