Skip to content

Lesson 4 of 9 · 27m

Node.js Compatibility: The Edges That Matter

Evaluate Bun against real dependency trees, native modules, Node built-ins, and ecosystem assumptions instead of treating compatibility as a yes-or-no checkbox.

Bun's Node compatibility is good enough that the failures surprise people. Most application code runs. Then one queue library assumes precise stream ordering, one native addon expects Node-API behavior, or one build tool shells out to node explicitly. Compatibility work is not arguing about percentages on a website; it is inventorying the assumptions in your dependency tree.

Build a compatibility inventory first

Before migrating, inspect the packages that sit below your framework:

bun pm ls --all | head -80
jq '.dependencies, .devDependencies' package.json
find node_modules -name '*.node' | head

Native .node files deserve immediate attention. So do packages with postinstall scripts, binary downloads, sharp/image processing, database drivers, or cloud SDKs. The question is not whether they are popular. It is whether they rely on implementation details Bun has not matched exactly.

Common failure families

Node-API/native addons       missing ABI or subtle lifecycle differences
stream edge behavior         backpressure, ordering, error propagation
process control              signals, execPath, child_process details
module internals             require.cache mutation or monkey patching
V8-specific APIs             vm behavior, inspector, heap snapshots
package exports              different condition resolution than expected

These are edge families, not a prediction that every package fails. The point is to test the riskiest surfaces deliberately instead of running the entire test suite and reading tea leaves.

Write a smoke harness for critical dependencies

A small executable matrix gives better evidence than a migration blog post:

import { describe, expect, test } from "bun:test";

test("redis client round trip", async () => {
  const value = await roundTripThroughRedis("compat-key", "ok");
  expect(value).toBe("ok");
});

test("postgres parameter binding", async () => {
  const rows = await sql`select ${"42"}::int as answer`;
  expect(rows[0].answer).toBe(42);
});

Exercise authentication, TLS, cancellation, large payloads, timeouts, and error paths. Success on the happy path tells you almost nothing about operational compatibility.

Beware transitive node invocations

Some tools are compatible with Bun's JavaScript execution but still spawn Node:

{
  "scripts": {
    "codegen": "node scripts/codegen.js",
    "migrate": "node ./node_modules/drizzle-kit/bin.cjs"
  }
}

That is not automatically wrong. It is a dependency on two runtimes. If production containers only install Bun, these scripts fail there. Either change the invocation intentionally, install Node in the environment, or keep the task in CI where both are available.

Use feature detection at boundaries

When behavior truly differs, detect capability rather than runtime branding:

const supportsImportAttributes = await import("./probe.mjs")
  .then(() => true)
  .catch(() => false);

Runtime detection (if (typeof Bun !== "undefined")) is sometimes necessary, but it rots quickly. Capability checks survive backports and fixes.

Field notes

  • Compatibility failures often occur only under load, cancellation, or stream backpressure; unit-test startup is insufficient.
  • Search your dependency tree for .node binaries before selecting Bun for a production service.
  • A package can install successfully and still fail when executed because install scripts and runtime APIs are different surfaces.
  • Keep a small compatibility suite that can run in CI after every Bun upgrade; behavior improves and occasionally regresses.

Was this lesson helpful?

Sign in to rate