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

> 使用 `bun publish` 将包发布到 npm 注册表

`bun publish` 将你的包打包成 tarball，从 `package.json` 中去除 catalog 和 workspace 协议（必要时解析版本），并发布到配置文件中指定的注册表。支持 `bunfig.toml` 和 `.npmrc` 文件。

```sh terminal icon="terminal" theme={null}
## 从当前工作目录发布包
bun publish
```

```txt theme={null}
bun publish v1.3.3 (ca7428e9)

打包了 203B package.json
打包了 224B README.md
打包了 30B index.ts
打包了 0.64KB tsconfig.json

总文件数：4
Shasum：79e2b4377b63f4de38dc7ea6e5e9dbee08311a69
Integrity：sha512-6QSNlDdSwyG/+[...]X6wXHriDWr6fA==
解压后大小：1.1KB
打包后大小：0.76KB
标签：latest
访问权限：default
注册表：http://localhost:4873/

 + publish-1@1.0.0
```

要分别打包和发布，先运行 `bun pm pack`，然后使用输出 tarball 的路径运行 `bun publish`。

```sh terminal icon="terminal" theme={null}
bun pm pack
...
bun publish ./package.tgz
```

<Note>
  如果提供了 tarball 路径，`bun publish` 不会运行生命周期脚本（`prepublishOnly/prepack/prepare/postpack/publish/postpublish`）。仅当 `bun publish` 自己打包时才会运行脚本。
</Note>

### `--access`

`--access` 设置正在发布的包的访问级别，可以是 `public` 或 `restricted`。无作用域的包始终是公开的，使用 `--access restricted` 发布无作用域包会报错。

```sh terminal icon="terminal" theme={null}
bun publish --access public
```

`--access` 也可以在 `package.json` 的 `publishConfig` 字段中设置。

```json package.json icon="file-json" theme={null}
{
  "publishConfig": {
    "access": "restricted"
  }
}
```

### `--tag`

设置正在发布的包版本的标签。默认情况下，标签是 `latest`。包的初始版本除了指定标签外，始终会被赋予 `latest` 标签。

```sh terminal icon="terminal" theme={null}
bun publish --tag alpha
```

`--tag` 也可以在 `package.json` 的 `publishConfig` 字段中设置。

```json package.json icon="file-json" theme={null}
{
  "publishConfig": {
    "tag": "next"
  }
}
```

### `--dry-run`

`--dry-run` 运行发布过程但不实际发布包，因此你可以验证将发布的内容。

```sh terminal icon="terminal" theme={null}
bun publish --dry-run
```

### `--tolerate-republish`

如果包版本已存在，以退出码 0 而非 1 退出。在可能重新运行的 CI/CD 作业中很有用。

```sh terminal icon="terminal" theme={null}
bun publish --tolerate-republish
```

### `--gzip-level`

设置打包包时使用的 gzip 压缩级别，范围为 `0` 到 `9`（默认为 `9`）。仅适用于没有 tarball 路径参数的 `bun publish`。

### `--auth-type`

如果你的 npm 账户启用了双因素认证，`bun publish` 会提示你输入一次性密码，可以通过浏览器或 CLI。`--auth-type` 告诉 npm 注册表你喜欢的方式：`web`（默认）或 `legacy`。

```sh terminal icon="terminal" theme={null}
bun publish --auth-type legacy
...
此操作需要一次性密码。
输入 OTP：123456
...
```

### `--otp`

直接向 CLI 提供一次性密码。如果密码有效，`bun publish` 会在发布前跳过额外的一次性密码提示：

```sh terminal icon="terminal" theme={null}
bun publish --otp 123456
```

<Note>
  `bun publish` 会读取 `NPM_CONFIG_TOKEN` 环境变量，这在从 GitHub Actions 或其他自动化工作流发布时很有用。
</Note>

***

## CLI 用法

```bash terminal icon="terminal" theme={null}
bun publish dist
```

### 发布选项

<ParamField path="--access" type="string">
  设置所发布包的访问级别，`public` 或 `restricted`。无作用域的包始终是 public；使用 `--access restricted` 发布无作用域包会导致错误。

  ```sh terminal icon="terminal" theme={null}
  bun publish --access public
  ```

  `--access` 也可以在 `package.json` 的 `publishConfig` 字段中设置。

  ```json package.json icon="file-json" theme={null}
  {
    "publishConfig": {
      "access": "restricted" // [!code ++]
    }
  }
  ```
</ParamField>

<ParamField path="--tag" type="string" default="latest">
  设置所发布包版本的标签。默认标签为 `latest`。包的初始版本除了指定标签外，始终会获得 `latest` 标签。

  ```sh terminal icon="terminal" theme={null}
  bun publish --tag alpha
  ```

  `--tag` 也可以在 `package.json` 的 `publishConfig` 字段中设置。

  ```json package.json icon="file-json" theme={null}
  {
    "publishConfig": {
      "tag": "next" // [!code ++]
    }
  }
  ```
</ParamField>

<ParamField path="--dry-run" type="boolean">
  模拟发布过程而不实际发布包，以先验证其内容。

  ```sh theme={null}
  bun publish --dry-run
  ```
</ParamField>

<ParamField path="--gzip-level" type="string" default="9">
  指定打包时使用的 gzip 压缩级别。仅适用于不带 tarball 路径参数的 `bun publish`。值范围为 `0` 到 `9`（默认 `9`）。
</ParamField>

<ParamField path="--auth-type" type="string" default="web">
  如果你的 npm 账户启用了 2FA，`bun publish` 会提示你输入一次性密码，可以通过浏览器或 CLI 输入。`--auth-type` 告诉 npm 注册表你偏好的方法：`web`（默认）或 `legacy`。

  ```sh terminal icon="terminal" theme={null}
  bun publish --auth-type legacy
  ...
  此操作需要一次性密码。
  Enter OTP: 123456
  ...
  ```
</ParamField>

<ParamField path="--otp" type="string">
  直接向 CLI 提供一次性密码。有效密码会在发布前跳过额外的一次性密码提示。

  ```sh terminal icon="terminal" theme={null}
  bun publish --otp 123456
  ```

  <Note>
    `bun publish` 会检查 `NPM_CONFIG_TOKEN` 环境变量，因此你可以从 GitHub Actions 或其他自动化工作流中发布。
  </Note>
</ParamField>

### 注册表配置

#### 自定义注册表

<ParamField path="--registry" type="string">
  指定注册表 URL，覆盖 .npmrc 和 bunfig.toml
</ParamField>

```bash theme={null}
bun publish --registry https://my-private-registry.com
```

#### SSL 证书

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

<ParamField path="--cafile" type="string">
  证书颁发机构证书文件路径
</ParamField>

<CodeGroup>
  ```bash 内联证书 theme={null}
  bun publish --ca "-----BEGIN CERTIFICATE-----..."
  ```

  ```bash 证书文件 theme={null}
  bun publish --cafile ./ca-cert.pem
  ```
</CodeGroup>

### 发布选项

#### 依赖管理

<ParamField path="-p, --production" type="boolean">
  不安装 devDependencies
</ParamField>

<ParamField path="--omit" type="string">
  排除依赖类型：`dev`、`optional` 或 `peer`
</ParamField>

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

#### 脚本控制

<ParamField path="--ignore-scripts" type="boolean">
  打包和发布期间跳过生命周期脚本
</ParamField>

<ParamField path="--trust" type="boolean">
  将包添加到 trustedDependencies 并运行其脚本
</ParamField>

<Note>
  **生命周期脚本** — 当你发布预构建的 tarball 时，Bun 不会运行生命周期脚本，如 `prepublishOnly` 和 `prepack`；它们仅在 Bun 自行打包时运行。
</Note>

#### 文件管理

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

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

<ParamField path="--yarn" type="boolean">
  生成 yarn.lock 文件（兼容 yarn v1）
</ParamField>

#### 性能

<ParamField path="--backend" type="string">
  平台优化：`clonefile`（默认）、`hardlink`、`symlink` 或 `copyfile`
</ParamField>

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

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