此版本修復了 69 個問題(獲得 204 個 👍)。
安裝 Bun
curl -fsSL https://bun.nodejs.com.tw/install | bashnpm install -g bunpowershell -c "irm bun.sh/install.ps1|iex"scoop install bunbrew tap oven-sh/bunbrew install bundocker pull oven/bundocker run --rm --init --ulimit memlock=-1:-1 oven/bun升級 Bun
bun upgradeBun.SQL - 統一的 SQL 客戶端
現在您可以使用 Bun.SQL 連線到 MySQL/MariaDB、SQLite 和 PostgreSQL。同一個資料庫庫可以用於三個最受歡迎的 SQL 資料庫,無需任何依賴。
Bun.SQL 中的 MySQL & MariaDB
Bun 的 MySQL/MariaDB 驅動程式是用 Zig 編寫的。

要開始使用,您可以將選項物件或 URL 字串傳遞給 SQL 建構函式。
import { SQL } from "bun";
// Connect to a MySQL or MariaDB database
const sql = new SQL({
adapter: "mysql",
hostname: "127.0.0.1",
username: "user",
password: "password",
database: "buns_burgers",
});
// Run a query
const users = await sql`SELECT * FROM users;`.all();
console.log(users);
// or via url
const mysql = new SQL("mysql://user:password@127.0.0.1:3306/buns_burgers");
// Run a query
const users = await mysql`SELECT * FROM users;`;
console.log(users);
感謝 @cirospaciari 的貢獻
Bun.SQL 中的 SQLite
Bun.SQL 現在還內建了 SQLite 支援。這為 SQLite 使用者帶來了以前僅適用於 PostgreSQL 的簡單且高效能的標記模板字串 API。
您可以透過傳遞 ":memory:" 來建立一個記憶體資料庫,或者透過提供像 sqlite://path/to/db.sqlite 這樣的路徑來建立一個基於檔案的資料庫。
import { SQL } from "bun";
// Create an in-memory SQLite database
const db = new SQL(":memory:");
// Create a table
await db`CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT)`;
// Insert multiple users
const names = ["Alice", "Bob"];
await db`INSERT INTO users (name) VALUES ${SQL.values(
names.map((name) => [name]),
)}`;
// Query the users
const users = await db`SELECT * FROM users ORDER BY name ASC`;
console.log(users);
// => [ { id: 1, name: 'Alice' }, { id: 2, name: 'Bob' } ]
感謝 @alii 的貢獻!
原生 YAML 支援
Bun 現在內建了 YAML 解析器。您可以直接匯入 .yaml 和 .yml 檔案,或者使用 Bun.YAML.parse 在執行時解析 YAML 字串。這與 Bun 對 JSON 和 TOML 的內建支援方式相同。
在下一版本的 Bun 中
— Bun (@bunjavascript) 2025年8月23日
JavaScript 迎來原生 YAML 支援
像 JSON 一樣輕鬆地在 JS 中匯入、打包、require 和解析 YAML pic.twitter.com/5TYRLDoZgM
要匯入 YAML 檔案,您可以像匯入 JavaScript 模組一樣 import 它。Bun 將解析檔案並將其內容匯出為預設匯出。
name: my-package
version: 1.1.1
import pkg from "./package.yaml";
console.log(pkg.name); // "my-package"
console.log(pkg.version); // "1.1.1"
要解析 YAML 字串,請使用 Bun.YAML.parse 函式。
import { YAML } from "bun";
const text = `
- item1
- item2
`;
const items = YAML.parse(text);
console.log(items); // [ "item1", "item2" ]
感謝 @dylan-conway 的貢獻!
postMessage(string) 速度提升 500 倍
現在,使用 postMessage 在 worker 之間傳送字串以及使用 structuredClone 克隆字串的速度得到了顯著提升。
Bun.secrets - CLI 工具的原生金鑰管理器
Bun.secrets 使用作業系統原生的憑據儲存來安全地儲存和檢索憑據。這有助於避免以明文檔案形式儲存敏感資料,對於 CLI 工具和本地開發尤其有用。
它在 macOS 上使用 Keychain Services,在 Linux 上使用 libsecret (GNOME Keyring, KWallet),在 Windows 上使用 Windows Credential Manager。所有操作(get、set 和 delete)都是非同步的,並在 Bun 的執行緒池中執行。
import { secrets } from "bun";
// Store a GitHub token securely
await secrets.set({
service: "my-cli-tool",
name: "github-token",
value: "ghp_xxxxxxxxxxxxxxxxxxxx",
});
// Retrieve it when needed
const token = await secrets.get({
service: "my-cli-tool",
name: "github-token",
});
// Or delete it later
await secrets.delete({
service: "my-cli-tool",
name: "github-token",
});
bun install 的安全掃描器 API
Bun 的包管理器現在可以在安裝前掃描包是否存在安全漏洞,有助於保護您的應用程式免受供應鏈攻擊和已知漏洞的侵害。
要啟用此功能,請從 npm 安裝一個安全掃描器包,並在您的 bunfig.toml 中進行配置。配置掃描器後,bun install 將呼叫它,如果在發現具有 fatal 嚴重性的漏洞時,安裝將被取消。
# bunfig.toml
[install.security]
# First, run `bun add -d @my-company/bun-security-scanner`
scanner = "@my-company/bun-security-scanner"
您可以在我們的文件中瞭解有關此 API 的更多資訊。有關構建安全掃描器的快速入門,請參閱我們的模板倉庫。
感謝 @alii 的貢獻!
bun audit 獲得新的過濾選項
bun audit 命令現在支援用於過濾漏洞的新標誌,讓您可以更精細地控制安全審計。
--audit-level=<level>:僅報告嚴重性為<level>或更高的漏洞。支援的級別包括low、moderate、high和critical。--prod:將審計限制為僅生產依賴項,忽略devDependencies。--ignore=<CVE>:允許您按 CVE ID 忽略特定漏洞。此標誌可以使用多次。
這些標誌使 bun audit 更易於整合到 CI/CD 管道中,因為它們允許您專注於與應用程式最相關的關鍵安全問題。
# Only report high and critical vulnerabilities
bun audit --audit-level=high
# Audit only production dependencies
bun audit --prod
# Ignore a specific vulnerability
bun audit --ignore CVE-2023-12345
# Ignore multiple vulnerabilities
bun audit --ignore CVE-2023-12345 --ignore CVE-2023-67890
感謝 @RiskyMH 的貢獻
bun install --lockfile-only 速度大幅提升
bun install --lockfile-only 命令已最佳化,避免從 npm 登錄檔中下載包 tarball。它現在僅獲取包清單,其中包含生成或更新 bun.lock 所需的所有資訊。
此更改顯著減少了網路頻寬使用量並提高了效能,尤其是在 CI 環境或具有許多依賴項的專案中。對於非 npm 依賴項,如直接 tarball URL 或 Git 儲存庫,tarball 仍將被下載以確保鎖定檔案的準確性。
bun update --interactive 支援滾動
bun update 的互動式模式(bun update -i 或 bun update --interactive)現在支援滾動。當可更新依賴項列表超過終端高度時,您可以使用向上和向下箭頭鍵導航完整列表。
此外,表格顯示現在可以響應您的終端寬度。長包名將被截斷,以確保在較小的螢幕上佈局保持整潔和可讀。
感謝 @RiskyMH 的貢獻
降低空閒 CPU 使用率
以前,Bun.serve 每秒會喚醒一次以更新 HTTP 響應的快取 Date 標頭。這導致 Bun 的程序即使在伺服器完全空閒或根本未使用時也會佔用少量 CPU 並觸發上下文切換。

現在,此計時器僅在有正在進行的請求時啟用。當伺服器空閒時,Bun 的程序將真正進入休眠狀態,消耗的 CPU 資源幾乎為零。
Bun.build() 現在支援編譯可執行檔案
您現在可以使用 Bun.build() JavaScript API 建立獨立的(standalone)可執行檔案。此功能以前僅透過 bun build --compile CLI 標誌可用,現在可以以程式設計方式訪問。在編譯可執行檔案時,打包器外掛也已全面支援。
Bun.build 中的新 compile 選項接受 true 以構建當前平臺,一個目標字串,如 'bun-linux-x64',或一個配置物件以獲得更高階的選項。
// Cross-compile an executable for Linux x64 with musl
await Bun.build({
entrypoints: ["./cli.ts"],
// target can be a shorthand string where it uses entrypoint name as executable name
compile: "bun-linux-x64-musl",
});
// Or use an object for more configuration, like setting a custom filename and Windows icon
await Bun.build({
entrypoints: ["./cli.ts"],
compile: {
target: "bun-windows-x64",
outfile: "./my-app-windows",
windows: {
// set the icon for the .exe
icon: "./icon.ico",
},
},
});
感謝 @RiskyMH 的貢獻
使用 --compile-exec-argv 將執行時標誌嵌入到獨立的(standalone)可執行檔案中
bun build --compile 命令現在支援新的 --compile-exec-argv 標誌,允許您將執行時引數直接嵌入到獨立的(standalone)可執行檔案中。
當編譯後的二進位制檔案執行時,這些引數將被 Bun 執行時像命令列引數一樣處理。這允許您建立具有不同執行時特徵的專用構建,例如啟用檢查器、設定預設使用者代理或最佳化記憶體使用。嵌入的引數也可透過 process.execArgv 進行檢查。
// index.ts
console.log(`Bun was launched with: ${process.execArgv.join(" ")}`);
// This --user-agent flag is actually processed by Bun's runtime.
// All fetch requests will use this user-agent.
const res = await fetch("https://api.bunjstest.com/agent");
console.log(`User-Agent header sent: ${await res.text()}`);
您可以使用嵌入的引數構建此檔案。
bun build ./index.ts --compile --outfile=my-app \ --compile-exec-argv="--smol --user-agent=MyApp/1.0"
./my-appBun was launched with: --smol --user-agent=MyApp/1.0
User-Agent header sent: MyApp/1.0為 Windows 可執行檔案設定元資料
您現在可以使用 bun build --compile 將元資料嵌入到 Windows 的獨立(standalone)可執行檔案中。這允許您設定應用程式的標題、釋出者、版本、描述和版權,這些資訊將在 Windows Explorer 的檔案屬性中可見。
這透過一組新的 CLI 標誌支援
--windows-title--windows-publisher--windows-version--windows-description--windows-copyright
這些選項也可透過 compile.windows 物件在 Bun.build() API 中使用。
await Bun.build({
entrypoints: ["./app.js"],
outfile: "./app.exe",
compile: {
windows: {
title: "My Cool App",
publisher: "My Company",
version: "1.2.3.4",
description: "This is a really cool application.",
copyright: "© 2024 My Company",
},
},
});
感謝 @RiskyMH 的貢獻
Bun.stripANSI() - SIMD 加速的 ANSI 轉義碼移除
Bun.stripANSI() 允許您從字串中剝離 ANSI 轉義碼。它作為 npm 的 strip-ansi 包的高效能內建替代品,速度比其快 6 倍到 57 倍。
const coloredText = "\u001b[31mHello\u001b[0m \u001b[32mWorld\u001b[0m";
const plainText = Bun.stripANSI(coloredText);
console.log(plainText); // => "Hello World"
// Works with various ANSI codes
const formatted = "\u001b[1m\u001b[4mBold and underlined\u001b[0m";
console.log(Bun.stripANSI(formatted)); // => "Bold and underlined"
感謝 @taylordotfish 的貢獻!
Windows 的程式碼簽名 bun.exe
Windows 的 bun.exe 現在已透過程式碼簽名,這解決了首次執行它時可能出現的安全警告。
感謝 @connerlphillippi 的貢獻!
bunx 現在支援 --package
您現在可以使用 --package (或 -p) 標誌與 bunx 一起執行包中的二進位制檔案,當二進位制檔案的名稱與包的名稱不同時。這對於包含多個二進位制檔案的包或作用域包很有用。這使得 bunx 的功能與 npx 和 yarn dlx 一致。
例如,renovate 包提供了一個 renovate-config-validator 二進位制檔案。您現在可以直接執行它
# Run the `renovate-config-validator` binary from the `renovate` package
bunx --package renovate renovate-config-validator
同樣,要使用 @angular/cli 包中的 ng 二進位制檔案
# Run the `ng` binary from the `@angular/cli` package
bunx -p @angular/cli ng new my-app
感謝 @RiskyMH 的貢獻
package.json 的 sideEffects 現在支援 glob 模式
Bun 的打包器現在支援在 package.json 的 sideEffects 欄位中使用 glob 模式。這允許更精確的 tree-shaking,並可以減小捆綁包大小,尤其是在使用依賴於此功能的元件庫時。
以前,使用 glob 模式會導致 Bun 對整個包進行反最佳化。現在,Bun 可以根據提供的模式正確識別哪些檔案具有副作用。
支援的模式包括 *、?、**、[] 和 {}。
// package.json
{
"name": "my-package",
"sideEffects": ["**/*.css", "./src/setup.js", "./src/components/*.js"]
}
在上面的示例中,Bun 將正確保留所有 CSS 檔案、src/setup.js 以及 src/components/ 中直接的任何 JavaScript 檔案,同時在未使用的模組時進行 tree-shaking。
感謝 @RiskyMH 的貢獻
使用 --user-agent 標誌自定義 User-Agent
Bun CLI 已新增新的 --user-agent 標誌。這允許您覆蓋使用 fetch() 在您的應用程式中發出的所有 HTTP 請求的預設 User-Agent 標頭。這對於標識您的應用程式到外部服務或需要特定 User-Agent 的 API 非常有用。
const response = await fetch("https://httpbin.org/user-agent");
const data = await response.json();
console.log(data["user-agent"]);
# Run with a custom user agentbun --user-agent "MyCustomApp/1.0" agent.jsMyCustomApp/1.0
# Without the flag, it uses the defaultbun agent.jsBun/1.2.18Node.js 相容性改進
- 修復:使用
ws時可能出現的TypeError,當 WebSocket 升級被中止或失敗時(伺服器端) - 修復:在 Worker 中使用
process.exit()現在可以正確通知 N-API 呼叫者 VM 正在終止,提高了 Node.js 相容性。 - 修復:在使用觸發垃圾回收的 NAPI 外掛(如
node-sqlite3)在其 finalizer 中時,程序退出時發生斷言失敗。 - 修復:在物件 finalizer 中呼叫
napi_is_exception_pending時,在原生外掛中可能發生的斷言失敗。 - 修復:使用
utf16le或ucs2編碼讀取奇數字節檔案時發生的斷言失敗。 - 修復:終止呼叫了
process.exit的 Worker 時發生的斷言失敗。 - 修復:在使用 N-API 原生外掛(如
lmdb)嘗試刪除已刪除或從未新增的清理鉤子時發生的斷言失敗。Bun 的行為現在與 Node.js 一致,Node.js 會靜默忽略此操作。 - 修復:
child_process中當 stdio 流被銷燬過快時發生的 bug。Bun 現在像 Node.js 一樣處理stream.Readable和stream.Writable例項。 - 修復:當
Error.prepareStackTrace使用null、undefined或其他非陣列值作為第二個引數時發生的 bug。 - 修復:從 N-API 模組丟擲的異常傳播到 JavaScript 過快,這可能導致崩潰的 bug。
- 修復:
node:readline/promises中的readline.createInterface()未實現[Symbol.dispose],導致無法與using宣告一起使用。 - 修復:
structuredClone中的 bug,其中某些非可傳輸物件被錯誤處理。 - 修復:
net.BlockList中的死鎖,當同一例項同時從多個執行緒訪問時可能發生。 - 修復:程序間通訊 (IPC) 中可能導致使用
node:cluster時崩潰的罕見競態條件。 - 修復:
node:crypto中的迴歸 bug,使用小寫演算法名稱(如'rsa-sha256')時,crypto.sign()等函式會失敗。這提高了與 Node.js 的相容性並修復了mailauth等庫。
打包器和解析器 bug 修復
- 修復:在 JSX 元素中解析無效模板字串(例如
<div a=\`/>`)時,JavaScript 詞法分析器中的斷言失敗。 - 修復:打包包含大浮點數值的樣式表(例如 TailwindCSS 的
rounded-full實用程式生成的)時,CSS 解析器中的斷言失敗。 - 修復了在 Bun 的前端開發伺服器中渲染多個前端錯誤時會丟擲
String contains an invalid character的 bug。
Bun.SQL bug 修復
- 修復:Bun.SQL 錯誤類現在都已匯出,可在
Bun.SQL.PostgresError、Bun.SQL.SQLiteError和Bun.SQL.MySQLError下訪問。這允許進行型別安全的錯誤處理,例如error instanceof Bun.SQL.PostgresError。您也可以使用它們都繼承的超類Bun.SQL.SQLError。 - 修復:在使用 PostgreSQL 時,在某些配置下 Unix 域套接字連線會失敗,通常導致
PostgresError: Connection closed。 sql(["a", "b", "c"])現在支援WHERE IN子句中的字串陣列。感謝 @zenazn 的貢獻。
bun install bug 修復
- 修復:已解決
bun update --interactive中的 UI 閃爍問題,提供更流暢的體驗。 - 修復:
yarn.lock遷移包的 long version strings,例如包含 git commit SHAs 的包 - 修復:處理源位置時,Bun 日誌記錄器中的斷言失敗。
- 修復:在
bun.lockb檔案中解析格式錯誤或過大的完整性雜湊時,bun install中的斷言失敗 - 修復:解析包含頂級目錄配置的
package.json時可能發生的斷言失敗。 - 修復:
bun init在建立檔案的摘要中重複列出CLAUDE.md的 bug。
執行時 bug 修復
- 修復:
bun upgrade錯誤訊息中的拼寫錯誤,該錯誤在驗證失敗時列印了額外的右括號,已得到糾正。 - 修復:
Bun.hash.xxHash64以正確處理大於 32 位的BigIntseed - 修復:
Bun.serve()會錯誤地拒絕包含單個配置物件的tls陣列 - 修復:在 Windows 上使用
bun shell($) 中的管道時受影響的 bug - 修復:在處理請求時呼叫
Bun.serve例項的server.stop()時可能發生的 bug - 修復:
Bun.serve即使已存在Date標頭,仍會將其新增到 HTTP 響應中,導致重複Date標頭的 bug - 修復:
HTMLRewriter中元素處理程式丟擲異常時的崩潰 bug。此更改還解決了解析無效 CSS 選擇器時的記憶體洩漏問題,並改進了它們的錯誤訊息。 - 修復:名稱空間匯入物件(
import * as ns from '...')錯誤地繼承了Object.prototype。如果這破壞了任何內容,請告知我們 - 改進了
bun:sqlite中的內部異常處理 expect(...).toBeCloseTo(...)現在已正確計入bun test報告的總 expect 呼叫次數。