Bun

指南套件管理器

從 npm install 遷移到 bun install

bun install 是一個與 Node.js 相容的 npm 客戶端,旨在成為 npm 的極速後繼者。

我們投入了大量工作,以確保從 npm install 遷移到 bun install 的路徑盡可能簡單,就像執行 bun install 而不是 npm install 一樣容易。

  • 專為 Node.js 和 Bun 設計bun install 安裝一個與 Node.js 相容的 node_modules 資料夾。您可以將其取代 npm install 用於 Node.js 專案,無需任何程式碼變更,也無需使用 Bun 的執行時環境。
  • 自動轉換 package-lock.json 為 bun 的 bun.lock 鎖定檔格式,保留您現有的已解析相依性版本,無需您進行任何手動操作。您可以偷偷地在工作場所使用 bun install 來取代 npm install,而不會有人注意到。
  • .npmrc 相容:bun install 從 npm 的 .npmrc 讀取 npm 登錄檔配置,因此您可以對 npm 和 Bun 使用相同的配置。
  • 硬連結:在 Windows 和 Linux 上,bun install 使用硬連結來節省磁碟空間和安裝時間。
# It only takes one command to migrate
bun i

# To add dependencies:
bun i @types/bun

# To add devDependencies:
bun i -d @types/bun

# To remove a dependency:
bun rm @types/bun

更快地執行 package.json 指令碼

從 package.json 執行指令碼、從 node_modules/.bin 執行可執行檔(有點像 npx),以及 JavaScript/TypeScript 檔案(就像 node)— 所有這些都來自一個簡單的指令。

NPMBun
npm run <script>bun <script>
npm exec <bin>bun <bin>
node <file>bun <file>
npx <package>bunx <package>

當您使用 bun run <executable> 時,它會選擇本地安裝的可執行檔

# Run a package.json script:
bun my-script
bun run my-script

# Run an executable in node_modules/.bin:
bun my-executable # such as tsc, esbuild, etc.
bun run my-executable

# Run a JavaScript/TypeScript file:
bun ./index.ts

工作區?是的。

bun install 支援工作區,與 npm 類似,但功能更多。

在 package.json 中,您可以將 "workspaces" 設定為相對路徑的陣列。

package.json
{
  "name": "my-app",
  "workspaces": ["packages/*", "apps/*"]
}

依工作區名稱篩選指令碼

在 Bun 中,--filter 標記接受 glob 模式,並將針對所有工作區套件並行執行指令,其中 name 符合該模式,並尊重相依性順序。

bun --filter 'lib-*' my-script
# instead of:
# npm run --workspace lib-foo --workspace lib-bar my-script

更新相依性

若要更新相依性,您可以使用 bun update <package>。這會將相依性更新到滿足 package.json 中指定的 semver 範圍的最新版本。

# Update a single dependency
bun update @types/bun

# Update all dependencies
bun update

# Ignore semver, update to the latest version
bun update @types/bun --latest

# Update a dependency to a specific version
bun update @types/bun@1.2.5

# Update all dependencies to the latest versions
bun update --latest

檢視過時的相依性

若要檢視過時的相依性,請執行 bun outdated。這類似於 npm outdated,但輸出更精簡。

bun outdated
┌────────────────────────────────────────┬─────────┬────────┬────────┐
│ Package                                │ Current │ Update │ Latest │
├────────────────────────────────────────┼─────────┼────────┼────────┤
│ @types/bun (dev)                       │ 1.1.6   │ 1.1.10 │ 1.1.10 │
├────────────────────────────────────────┼─────────┼────────┼────────┤
│ @types/react (dev)                     │ 18.3.3  │ 18.3.8 │ 18.3.8 │
├────────────────────────────────────────┼─────────┼────────┼────────┤
│ @typescript-eslint/eslint-plugin (dev) │ 7.16.1  │ 7.18.0 │ 8.6.0  │
├────────────────────────────────────────┼─────────┼────────┼────────┤
│ @typescript-eslint/parser (dev)        │ 7.16.1  │ 7.18.0 │ 8.6.0  │
├────────────────────────────────────────┼─────────┼────────┼────────┤
│ @vscode/debugadapter (dev)             │ 1.66.0  │ 1.67.0 │ 1.67.0 │
├────────────────────────────────────────┼─────────┼────────┼────────┤
│ esbuild (dev)                          │ 0.21.5  │ 0.21.5 │ 0.24.0 │
├────────────────────────────────────────┼─────────┼────────┼────────┤
│ eslint (dev)                           │ 9.7.0   │ 9.11.0 │ 9.11.0 │
├────────────────────────────────────────┼─────────┼────────┼────────┤
│ mitata (dev)                           │ 0.1.11  │ 0.1.14 │ 1.0.2  │
├────────────────────────────────────────┼─────────┼────────┼────────┤
│ prettier-plugin-organize-imports (dev) │ 4.0.0   │ 4.1.0  │ 4.1.0  │
├────────────────────────────────────────┼─────────┼────────┼────────┤
│ source-map-js (dev)                    │ 1.2.0   │ 1.2.1  │ 1.2.1  │
├────────────────────────────────────────┼─────────┼────────┼────────┤
│ typescript (dev)                       │ 5.5.3   │ 5.6.2  │ 5.6.2  │
└────────────────────────────────────────┴─────────┴────────┴────────┘

列出已安裝的套件

若要列出已安裝的套件,您可以使用 bun pm ls。這會列出使用 Bun 的鎖定檔作為事實來源在 node_modules 資料夾中安裝的所有套件。您可以傳遞 -a 標記來列出所有已安裝的套件,包括過渡性相依性。

# List top-level installed packages:
bun pm ls
my-pkg node_modules (781)
├── @types/node@20.16.5
├── @types/react@18.3.8
├── @types/react-dom@18.3.0
├── eslint@8.57.1
├── eslint-config-next@14.2.8

# List all installed packages:
bun pm ls -a
my-pkg node_modules
├── @alloc/quick-lru@5.2.0
├── @isaacs/cliui@8.0.2
│   └── strip-ansi@7.1.0
│       └── ansi-regex@6.1.0
├── @jridgewell/gen-mapping@0.3.5
├── @jridgewell/resolve-uri@3.1.2
...

建立套件 tarball

若要建立套件 tarball,您可以使用 bun pm pack。這會在目前目錄中建立套件的 tarball。

# Create a tarball
bun pm pack

Total files: 46
Shasum: 2ee19b6f0c6b001358449ca0eadead703f326216
Integrity: sha512-ZV0lzWTEkGAMz[...]Gl4f8lA9sl97g==
Unpacked size: 0.41MB
Packed size: 117.50KB

Shebang

如果套件在 #!/usr/bin/env node shebang 中引用了 node,則 bun run 預設會尊重它並使用系統的 node 可執行檔。您可以強制它使用 Bun 的 node,方法是將 --bun 傳遞給 bun run

當您將 --bun 傳遞給 bun run 時,我們會建立一個符號連結到本地安裝的 Bun 可執行檔,名為 "node",在一個臨時目錄中,並將其新增到您的 PATH 中,以供指令碼執行期間使用。

# Force using Bun's runtime instead of node
bun --bun my-script

# This also works:
bun run --bun my-script

全域安裝

您可以使用 bun i -g <package> 全域安裝套件。這預設會安裝到您主目錄內的 .bun/install/global/node_modules 資料夾中。

# Install a package globally
bun i -g eslint

# Run a globally-installed package without the `bun run` prefix
eslint --init