bun prune
移除不在 bun.lock 中的包
bun prune 會刪除 node_modules 中當前 bun.lock 不會安裝的所有內容:例如切換分支、移除相依或使用其他套件管理器安裝後殘留的包。使用 isolated linker 時,這還包括 node_modules/.bun 中的陳舊條目。
bun prune 從不聯絡 registry,從不執行生命週期指令碼,也不會修改 bun.lock 或 package.json。
terminal
bun prune
Code
bun prune v1.4.0 (abc12345)
- @types/node@20.11.5
- left-pad@1.3.0
2 packages removed (checked 948 installed packages) [22.00ms]
從 workspace 或巢狀的 node_modules 資料夾中移除的包會在括號中顯示資料夾,例如 - typescript@5.4.0 (packages/app/node_modules)。
--production
同時移除 bun install --production 不會安裝的所有內容(即 devDependencies)。這樣你就可以使用 dev dependencies 進行建置,並在不包含它們的情況下發布:
Dockerfile
COPY package.json bun.lock ./
RUN bun install --frozen-lockfile
COPY . .
RUN bun run build
RUN bun prune --production
--omit=dev、--omit=optional 和 --omit=peer 的工作方式與用於 bun install 時相同。
--dry-run
列出將要移除的內容,但不實際刪除任何內容:
terminal
bun prune --production --dry-run
Code
bun prune v1.4.0 (abc12345)
- typescript@5.4.0
1 package can be removed (checked 948 installed packages) [9.00ms]
bun prune --production
--filter
僅清理所選 workspaces 的 node_modules 資料夾(使用與 bun install --filter 相同的模式)。Bun 也會清理共享位置:根目錄的 node_modules,或使用 isolated linker 時的 node_modules/.bun。在這些位置,Bun 會保留所選 workspace 仍然需要的所有內容。
terminal
bun prune --production --filter app
注意事項
- 始終從 workspace 根目錄執行,並覆蓋每個 workspace 的
node_modules,即使在 workspace package 內呼叫也是如此。 - 要求
bun.lock與package.json匹配。如果你在上次安裝後編輯了相依項,請先執行bun install。 - 會檢測
node_modules是由 hoisted linker 還是 isolated linker 佈局,並按當前佈局清理。如果資料夾中同時存在這兩種佈局(例如切換 linker 後未進行乾淨安裝),Bun 會使用bun install將使用的 linker。傳入--linker可進行選擇。 - 按包名匹配。如果包的版本不正確,Bun 會將其保留,等待
bun install替換。只有在上層已安裝正確版本時,Bun 才會移除巢狀副本(node_modules/a/node_modules/b);否則會保留該副本並列印警告。 - 從不移除 workspace 資料夾、仍在使用的
.bin條目、.cache等點目錄、普通檔案或node_modules之外的任何內容。 - 會移除針對當前
os/cpu停用的包。傳入--os/--cpu可針對其他平臺進行清理。 - 在已清理的 monorepo 檢出中(例如
turbo prune的輸出),其工作方式與bun install --frozen-lockfile相同。 - 如果任何條目刪除失敗,命令仍會移除其餘內容並以
1退出。 - 不支援
--global。 - 若要清理全域性快取,請改用
bun pm cache rm。