Lesson 2 of 9 · 24m
Runtime APIs, File I/O, and the Bun Standard Library
Use Bun-native APIs where they simplify real work, while keeping portability explicit at module boundaries.
The easiest Bun migration trap is stylistic: developers discover Bun.file, Bun.write, Bun.serve, and friends, then sprinkle them through business logic because they are pleasant. Six months later the service cannot run under Node, cannot be tested in the old harness, and has runtime assumptions hidden in invoice calculations. Runtime-specific APIs are a dependency. Put them behind boundaries.
Start with Web-standard shapes
Bun implements many browser-standard APIs directly. Prefer them when they express the operation cleanly:
const response = await fetch("https://api.github.com/repos/oven-sh/bun", {
headers: { "user-agent": "release-checker/1.0" },
});
if (!response.ok) {
throw new Error(`GitHub responded ${response.status}`);
}
const release = await response.json();fetch, Request, Response, Headers, URL, Blob, FormData, streams, and crypto primitives are increasingly the portable center of modern server JavaScript. Code written against those interfaces travels better than code built around either Node's legacy callback APIs or Bun-only conveniences.
Bun-native file operations
Bun's file API is ergonomic and often fast:
const input = Bun.file("config/server.toml");
const text = await input.text();
await Bun.write("dist/server.toml", text);Useful details matter here. Bun.file is lazy: creating the object does not read the file. Bun.write accepts strings, typed arrays, blobs, and other Bun file objects. These are good operational primitives for scripts, CLIs, and deployment tooling. But if a package might be consumed outside Bun, isolate the calls:
export interface ArtifactStore {
read(path: string): Promise<string>;
write(path: string, contents: string): Promise<void>;
}The interface gives you a Bun implementation today and a Node implementation tomorrow without rewriting policy code.
Bun.serve is not just a demo server
Bun's built-in server is production-capable for many HTTP workloads:
const server = Bun.serve({
port: 3000,
async fetch(request) {
const url = new URL(request.url);
if (url.pathname === "/healthz") return new Response("ok
");
return Response.json({ error: "not_found" }, { status: 404 });
},
});
console.log(`listening on ${server.port}`);The important architectural detail is the handler shape: it is close to the WinterCG-style fetch model. That is cleaner than framework lock-in, but it does not remove production concerns. Timeouts, body limits, reverse proxy behavior, observability, and graceful shutdown still belong in your design.
Node compatibility is broad, not identical
Bun implements large portions of node:fs, node:path, node:crypto, node:process, streams, buffers, and module resolution. Large portions is not the same as complete or bug-for-bug compatible. Native addons, obscure stream combinations, process control edge cases, and libraries that inspect V8 internals are common fault lines.
Field notes
- Prefer
node:prefixes for Node built-ins; they make intent explicit and avoid package-name shadowing accidents. - A file API being fast in a microbenchmark does not excuse unbounded reads. Memory behavior is still your responsibility.
- Keep Bun-specific imports out of shared domain libraries unless the dependency is deliberate.
- If a service may ever run on Node, test the same interface under both runtimes instead of relying on type checks.
Was this lesson helpful?