Skip to main content
bun prune 会删除 node_modules 中当前 bun.lock 不会安装的所有内容:例如切换分支、移除依赖或使用其他包管理器安装后残留的包。使用 isolated linker 时,这还包括 node_modules/.bun 中的陈旧条目。 bun prune 从不联系 registry,从不运行生命周期脚本,也不会修改 bun.lock 或 package.json。
terminal
从 workspace 或嵌套的 node_modules 文件夹中移除的包会在括号中显示文件夹,例如 - [email protected] (packages/app/node_modules)。

--production

同时移除 bun install --production 不会安装的所有内容(即 devDependencies)。这样你就可以使用 dev dependencies 进行构建,并在不包含它们的情况下发布:
--omit=dev、--omit=optional 和 --omit=peer 的工作方式与用于 bun install 时相同。

--dry-run

列出将要移除的内容,但不实际删除任何内容:
terminal

--filter

仅清理所选 workspaces 的 node_modules 文件夹(使用与 bun install --filter 相同的模式)。Bun 也会清理共享位置:根目录的 node_modules,或使用 isolated linker 时的 node_modules/.bun。在这些位置,Bun 会保留所选 workspace 仍然需要的所有内容。
terminal

注意事项

  • 始终从 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。