Skip to main content

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 pm 命令组提供了一套用于操作 Bun 包管理器的实用工具。

pack

创建当前工作区的 tar 包:
terminal
bun pm pack
此命令创建一个 .tgz 文件,包含所有将发布到 npm 的文件,规则与 npm pack 相同。

示例

基本用法:
terminal
bun pm pack
# 在当前目录创建 my-package-1.0.0.tgz
脚本静默模式:
terminal
TARBALL=$(bun pm pack --quiet)
echo "Created: $TARBALL"
Created: my-package-1.0.0.tgz
自定义目标目录:
terminal
bun pm pack --destination ./dist
# 在 ./dist/ 目录保存 tar 包

选项

  • --dry-run:执行所有操作但不写入 tar 包到磁盘,显示将包含的内容。
  • --destination <dir>:指定 tar 包保存的目录。
  • --filename <name>:指定 tar 包保存的精确文件名。
  • --ignore-scripts:跳过运行 prepack、postpack 和 prepare 脚本。
  • --gzip-level <0-9>:设置 gzip 压缩级别,范围 0 到 9(默认 9)。
  • --quiet:仅输出 tar 包文件名,抑制详细输出,适合脚本和自动化。
注意: --filename--destination 不能同时使用。

输出模式

默认输出:
terminal
bun pm pack
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
静默输出:
terminal
bun pm pack --quiet
my-package-1.0.0.tgz
--quiet 参数在自动化流程中非常有用,可直接获取生成的 tar 包文件名以便后续处理。

bin

打印当前项目的 bin 目录路径:
terminal
bun pm bin
/path/to/current/project/node_modules/.bin
打印全局 bin 目录路径:
terminal
bun pm bin -g
<$HOME>/.bun/bin

ls

打印当前项目已安装依赖及解析版本,不包含它们的依赖。
terminal
bun pm ls
# 或者
bun list
/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
打印所有已安装依赖,包括多级依赖:
terminal
bun pm ls --all
# 或者
bun list --all
/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 中保存凭据:
terminal
bun pm whoami

hash

生成并打印当前锁文件的哈希:
terminal
bun pm hash
打印用于哈希锁文件的字符串:
terminal
bun pm hash-string
打印当前锁文件中存储的哈希:
terminal
bun pm hash-print

cache

打印 Bun 的全局模块缓存路径:
terminal
bun pm cache
清除 Bun 的全局模块缓存:
terminal
bun pm cache rm

migrate

迁移其他包管理器的锁文件,不安装任何东西:
terminal
bun pm migrate

untrusted

打印当前带有脚本的未受信任依赖:
terminal
bun pm untrusted
./node_modules/@biomejs/biome @1.8.3
 » [postinstall]: node scripts/postinstall.js

这些依赖在安装时其生命周期脚本被阻止运行。

trust

运行未受信任依赖的脚本并添加到 trustedDependencies
terminal
bun pm trust <names>
trust 命令选项:
  • --all:信任所有未受信任的依赖。

default-trusted

打印默认可信赖依赖列表:
terminal
bun pm default-trusted
查看当前列表可访问 GitHub 这里

version

显示当前包版本及帮助信息:
terminal
bun pm version
bun pm version v1.3.3 (ca7428e9)
当前包版本:v1.0.0

版本增量:
  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      设置指定版本

选项:
  --no-git-tag-version 跳过 git 操作
  --allow-same-version 避免版本相同时报错
  --message=<val>, -m  自定义提交信息,使用 %s 代替版本号
  --preid=<val>        预发行标识(例如 beta → 1.0.1-beta.0)
  --force, -f          跳过检查 git 是否脏状态

示例:
  bun pm version patch
  bun pm version 1.2.3 --no-git-tag-version
  bun pm version prerelease --preid beta --message "Release beta: %s"
package.json 中升级版本:
terminal
bun pm version patch
v1.0.1
支持 patchminormajorpremajorpreminorprepatchprereleasefrom-git 或具体版本如 1.2.3。默认会创建 git 提交和标签,除非使用 --no-git-tag-version 跳过。

pkg

管理 package.json 数据,可进行获取、设置、删除及修复操作。 所有命令支持点号和中括号路径表示法:
terminal
scripts.build              # 点号表示法
contributors[0]            # 访问数组项
workspaces.0               # 数字索引的点号表示法
scripts[test:watch]        # 中括号表示特殊字符
示例:
terminal
# 获取
bun pm pkg get name                               # 单个属性
bun pm pkg get name version                       # 多个属性
bun pm pkg get                                    # 整个 package.json
bun pm pkg get scripts.build                      # 嵌套属性

# 设置
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 值

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

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