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整理維護