Bun

Node.js 相容性

每天,Bun 都更接近 100% Node.js API 相容性。如今,諸如 Next.js、Express 等流行的框架,以及數百萬個旨在用於 Node 的 npm 套件,都能與 Bun 協同運作。為了確保相容性,我們會在 Bun 的每個版本發布前,執行 Node.js 測試套件中的數千個測試。

如果一個套件在 Node.js 中可以運作,但在 Bun 中卻無法運作,我們會將其視為 Bun 的錯誤。開啟 issue,我們會修復它。

此頁面會定期更新,以反映最新版本 Bun 的相容性狀態。以下資訊反映 Bun 與Node.js v23的相容性。

內建 Node.js 模組

node:assert

🟢 完全實作。

node:buffer

🟢 完全實作。

node:console

🟢 完全實作。

node:dgram

🟢 完全實作。通過 Node.js 測試套件中超過 90% 的測試。

node:diagnostics_channel

🟢 完全實作。

node:dns

🟢 完全實作。通過 Node.js 測試套件中超過 90% 的測試。

node:events

🟢 完全實作。EventEmitterAsyncResource 底層使用 AsyncResource。EventEmitter 的 Node.js 測試套件通過率達 100%。

node:fs

🟢 完全實作。通過 Node.js 測試套件中 92% 的測試。

node:http

🟢 完全實作。傳出的客戶端請求主體目前為緩衝而非串流。

node:https

🟢 API 已實作,但 Agent 並非總是使用。

node:os

🟢 完全實作。Node.js 測試套件通過率達 100%。

node:path

🟢 完全實作。Node.js 測試套件通過率達 100%。

node:punycode

🟢 完全實作。Node.js 測試套件通過率達 100%,Node.js 已棄用

node:querystring

🟢 完全實作。Node.js 測試套件通過率達 100%。

node:readline

🟢 完全實作。

node:stream

🟢 完全實作。

node:string_decoder

🟢 完全實作。Node.js 測試套件通過率達 100%。

node:timers

🟢 建議改用全域的 setTimeout 等。

node:tty

🟢 完全實作。

node:url

🟢 完全實作。

node:zlib

🟢 已完整實作。通過 98% 的 Node.js 測試套件。

node:async_hooks

🟡 已實作 AsyncLocalStorageAsyncResource。v8 promise hooks 未被調用,且強烈建議不要使用

node:child_process

🟡 缺少 proc.gid proc.uid。未匯出 Stream 類別。IPC 無法傳送 socket handles。Node.js 與 Bun 之間的 IPC 可以使用 JSON 序列化。

node:cluster

🟡 Worker 之間無法傳遞 handles 和 file descriptors,這表示目前僅在 Linux 上支援跨進程的 HTTP 請求負載平衡 (透過 SO_REUSEPORT)。其他部分已實作但未經過實戰測試。

node:crypto

🟡 缺少 ECDH checkPrime checkPrimeSync generatePrime generatePrimeSync hkdf hkdfSync secureHeapUsed setEngine setFips

某些方法尚未最佳化。

node:domain

🟡 缺少 Domain active

node:http2

🟡 已實作客戶端和伺服器 (通過 95.25% 的 gRPC 測試套件)。缺少 options.allowHTTP1options.enableConnectProtocol、ALTSVC 擴展和 http2stream.pushStream

node:module

🟡 缺少 runMain syncBuiltinESMExportsModule#load()。支援覆寫 ESM 和 CJS 模組的 require.cachemodule._extensionsmodule._pathCachemodule._cache 為 no-ops。module.register 尚未實作,我們建議在此期間使用 Bun.plugin

node:net

🟡 SocketAddress 類別未公開 (但已實作)。BlockList 存在但為 no-op。

node:perf_hooks

🟡 缺少 createHistogram monitorEventLoopDelay。建議使用全域的 performance 而非 perf_hooks.performance

node:process

🟡 請參閱 process 全域物件。

node:sys

🟡 請參閱 node:util

node:tls

🟡 缺少 tls.createSecurePair

node:util

🟡 缺少 getCallSite getCallSites getSystemErrorMap getSystemErrorMessage transferableAbortSignal transferableAbortController MIMEType MIMEParams

node:v8

🟡 已實作 writeHeapSnapshotgetHeapSnapshotserializedeserialize 使用 JavaScriptCore 的 wire format 而非 V8 的格式。其他方法尚未實作。如需效能分析,請改用 bun:jsc

node:vm

🟡 核心功能可運作,但實驗性的 VM ES 模組尚未實作,包括 vm.Modulevm.SourceTextModulevm.SyntheticModuleimportModuleDynamicallyvm.measureMemorytimeoutbreakOnSigintcachedData 等選項尚未實作。

node:wasi

🟡 部分實作。

node:worker_threads

🟡 Worker 不支援以下選項:stdin stdout stderr trackedUnmanagedFds resourceLimits。缺少 markAsUntransferable moveMessagePortToContext getHeapSnapshot

node:inspector

🔴 尚未實作。

node:repl

🔴 尚未實作。

node:sqlite

🔴 尚未實作。

node:test

🔴 尚未實作。請改用 bun:test

node:trace_events

🔴 尚未實作。

Node.js 全域物件

下表列出 Node.js 實作的所有全域物件以及 Bun 目前的相容性狀態。

AbortController

🟢 完全實作。

AbortSignal

🟢 完全實作。

Blob

🟢 完全實作。

Buffer

🟢 完全實作。

ByteLengthQueuingStrategy

🟢 完全實作。

__dirname

🟢 完全實作。

__filename

🟢 完全實作。

atob()

🟢 完全實作。

BroadcastChannel

🟢 完全實作。

btoa()

🟢 完全實作。

clearImmediate()

🟢 完全實作。

clearInterval()

🟢 完全實作。

clearTimeout()

🟢 完全實作。

CompressionStream

🔴 尚未實作。

console

🟢 完全實作。

CountQueuingStrategy

🟢 完全實作。

Crypto

🟢 完全實作。

SubtleCrypto (crypto)

🟢 完全實作。

CryptoKey

🟢 完全實作。

CustomEvent

🟢 完全實作。

DecompressionStream

🔴 尚未實作。

Event

🟢 完全實作。

EventTarget

🟢 完全實作。

exports

🟢 完全實作。

fetch

🟢 完全實作。

FormData

🟢 完全實作。

global

🟢 已實作。這是一個包含全域命名空間中所有物件的物件。它很少被直接引用,因為它的內容在沒有額外前綴的情況下可用,例如 __dirname 而不是 global.__dirname

globalThis

🟢 global 的別名。

Headers

🟢 完全實作。

MessageChannel

🟢 完全實作。

MessageEvent

🟢 完全實作。

MessagePort

🟢 完全實作。

module

🟢 完全實作。

PerformanceEntry

🟢 完全實作。

PerformanceMark

🟢 完全實作。

PerformanceMeasure

🟢 完全實作。

PerformanceObserver

🟢 完全實作。

PerformanceObserverEntryList

🟢 完全實作。

PerformanceResourceTiming

🟢 完全實作。

performance

🟢 完全實作。

process

🟡 大部分已實作。process.binding (某些套件依賴的內部 Node.js 綁定) 為部分實作。process.title 目前在 macOS 和 Linux 上為 no-op。getActiveResourcesInfosetActiveResourcesInfogetActiveResourcessetSourceMapsEnabled 為虛設常式。較新的 API,如 process.loadEnvFileprocess.getBuiltinModule 尚未實作。

queueMicrotask()

🟢 完全實作。

ReadableByteStreamController

🟢 完全實作。

ReadableStream

🟢 完全實作。

ReadableStreamBYOBReader

🟢 完全實作。

ReadableStreamDefaultReader

🟢 完全實作。

ReadableStreamDefaultController

🟢 完全實作。

ReadableStreamBYOBRequest

🟢 完全實作。

require()

🟢 已完整實作,包括 require.mainrequire.cacherequire.resolverequire.extensions 為虛設常式。

Response

🟢 完全實作。

Request

🟢 完全實作。

setImmediate()

🟢 完全實作。

setInterval()

🟢 完全實作。

setTimeout()

🟢 完全實作。

structuredClone()

🟢 完全實作。

SubtleCrypto

🟢 完全實作。

DOMException

🟢 完全實作。

TextDecoder

🟢 完全實作。

TextDecoderStream

🟢 完全實作。

TextEncoder

🟢 完全實作。

TextEncoderStream

🟢 完全實作。

TransformStream

🟢 完全實作。

TransformStreamDefaultController

🟢 完全實作。

URL

🟢 完全實作。

URLSearchParams

🟢 完全實作。

WebAssembly

🟢 完全實作。

WritableStream

🟢 完全實作。

WritableStreamDefaultController

🟢 完全實作。

WritableStreamDefaultWriter

🟢 完全實作。