Skip to content

bun pm 命令组提供了一组用于使用 Bun 包管理器的工具。

pack

要创建当前工作区的 tarball:

bash
bun pm pack

此命令创建一个包含所有将发布到 npm 的文件的 .tgz 文件,遵循与 npm pack 相同的规则。

示例

基本用法:

bash
bun pm pack
# 在当前目录创建 my-package-1.0.0.tgz

用于脚本的静默模式:

bash
TARBALL=$(bun pm pack --quiet)
echo "已创建:$TARBALL"
txt
已创建:my-package-1.0.0.tgz

自定义目标:

bash
bun pm pack --destination ./dist
# 将 tarball 保存到 ./dist/ 目录

选项

  • --dry-run:执行除将 tarball 写入磁盘之外的所有任务。显示将包含的内容。
  • --destination <dir>:指定保存 tarball 的目录。
  • --filename <name>:指定 tarball 的确切文件名。
  • --ignore-scripts:跳过运行 pre/postpack 和 prepare 脚本。
  • --gzip-level <0-9>:设置 gzip 的自定义压缩级别,范围从 0 到 9(默认为 9)。
  • --quiet:仅输出 tarball 文件名,抑制详细输出。适用于脚本和自动化。

注意: --filename--destination 不能同时使用。

输出模式

默认输出:

bash
bun pm pack
txt
bun pack v1.2.19

packed 131B package.json
packed 40B index.js

my-package-1.0.0.tgz

Total files: 2
Shasum: f2451d6eb1e818f500a791d9aace80b394258a90
Unpacked size: 171B
Packed size: 249B

静默输出:

bash
bun pm pack --quiet
txt
my-package-1.0.0.tgz

--quiet 标志对于自动化工作流特别有用,你需要捕获生成的 tarball 文件名以进行进一步处理。

bin

要打印本地项目的 bin 目录路径:

bash
bun pm bin
txt
/path/to/current/project/node_modules/.bin

要打印全局 bin 目录的路径:

bash
bun pm bin -g
txt
<$HOME>/.bun/bin

ls

要打印当前项目中已安装依赖及其解析版本的列表,不包括它们的依赖。

bash
bun pm ls
# 或
bun list
txt
/path/to/project node_modules (135)
├── eslint@8.38.0
├── react@18.2.0
├── react-dom@18.2.0
├── typescript@5.0.4
└── zod@3.21.4

要打印所有已安装依赖,包括 n 级依赖。

bash
bun pm ls --all
# 或
bun list --all
txt
/path/to/project node_modules (135)
├── @eslint-community/eslint-utils@4.4.0
├── @eslint-community/regexpp@4.5.0
├── @eslint/eslintrc@2.0.2
├── @eslint/js@8.38.0
├── @nodelib/fs.scandir@2.1.5
├── @nodelib/fs.stat@2.0.5
├── @nodelib/fs.walk@1.2.8
├── acorn@8.8.2
├── acorn-jsx@5.3.2
├── ajv@6.12.6
├── ansi-regex@5.0.1
├── ...

whoami

打印你的 npm 用户名。需要你已登录(bunx npm login)并在 bunfig.toml.npmrc 中有凭据:

bash
bun pm whoami

hash

要生成并打印当前锁文件的哈希:

bash
bun pm hash

要打印用于哈希锁文件的字符串:

bash
bun pm hash-string

要打印存储在当前锁文件中的哈希:

bash
bun pm hash-print

cache

要打印 Bun 的全局模块缓存路径:

bash
bun pm cache

要清除 Bun 的全局模块缓存:

bash
bun pm cache rm

migrate

要在不安装任何内容的情况下迁移另一个包管理器的锁文件:

bash
bun pm migrate

untrusted

要打印当前具有脚本的不可信依赖:

bash
bun pm untrusted
txt
./node_modules/@biomejs/biome @1.8.3
 » [postinstall]: node scripts/postinstall.js

These dependencies had their lifecycle scripts blocked during install.

trust

为不可信依赖运行脚本并添加到 trustedDependencies

bash
bun pm trust <names>

trust 命令的选项:

  • --all:信任所有不可信依赖。

default-trusted

要打印默认受信任依赖列表:

bash
bun pm default-trusted

在 GitHub 上查看当前列表 这里

version

要显示当前包版本和帮助:

bash
bun pm version
txt
bun pm version v1.3.3 (ca7428e9)
Current package version: v1.0.0

Increment:
  patch      1.0.0 → 1.0.1
  minor      1.0.0 → 1.1.0
  major      1.0.0 → 2.0.0
  prerelease 1.0.0 → 1.0.1-0
  prepatch   1.0.0 → 1.0.1-0
  preminor   1.0.0 → 1.1.0-0
  premajor   1.0.0 → 2.0.0-0
  from-git   使用最新 git 标签的版本
  1.2.3      设置特定版本

Options:
  --no-git-tag-version 跳过 git 操作
  --allow-same-version 防止在版本相同时抛出错误
  --message=<val>, -m  自定义提交消息,使用 %s 进行版本替换
  --preid=<val>        预发布标识符(即 beta → 1.0.1-beta.0)
  --force, -f          绕过脏 git 历史检查

Examples:
  bun pm version patch
  bun pm version 1.2.3 --no-git-tag-version
  bun pm version prerelease --preid beta --message "发布 beta: %s"

要提升 package.json 中的版本:

bash
bun pm version patch
txt
v1.0.1

支持 patchminormajorpremajorpreminorprepatchprereleasefrom-git 或特定版本如 1.2.3。默认创建 git 提交和标签,除非使用 --no-git-tag-version 跳过。

pkg

使用 get、set、delete 和 fix 操作管理 package.json 数据。

所有命令都支持点和方括号表示法:

bash
scripts.build              # 点表示法
contributors[0]            # 数组访问
workspaces.0               # 带数字索引的点
scripts[test:watch]        # 用于特殊字符的方括号

示例:

bash
# get
bun pm pkg get name                               # 单个属性
bun pm pkg get name version                       # 多个属性
bun pm pkg get                                    # 整个 package.json
bun pm pkg get scripts.build                      # 嵌套属性

# set
bun pm pkg set name="my-package"                  # 简单属性
bun pm pkg set scripts.test="jest" version=2.0.0  # 多个属性
bun pm pkg set {"private":"true"} --json          # 使用 --json 标志的 JSON 值

# delete
bun pm pkg delete description                     # 单个属性
bun pm pkg delete scripts.test contributors[0]    # 多个/嵌套

# fix
bun pm pkg fix                                    # 自动修复常见问题

Bun学习网由www.bunjs.com.cn整理维护