Skip to content

How to understand a Bun codebase

Bun is a JavaScript runtime, package manager, bundler and test runner in one. Most Bun projects look like Node projects with `package.json`, but use Bun's built-in APIs for HTTP, files, SQLite and testing, which replace dependencies a Node app would install.

How to tell it's Bun

  • bun.lock (text lockfile since Bun 1.2) or the older binary bun.lockb
  • bunfig.toml, and bun run or bun test in scripts
  • Bun.serve, Bun.file and imports from bun:sqlite or bun:test

Where things usually live

package.json
Scripts and dependencies, as in Node.
bunfig.toml
Runtime, install and test settings.
src/index.ts
Common entry; may call Bun.serve directly.
*.test.ts
Tests using bun:test.

Conventions, not rules. Real projects rename and move things, so confirm against the repository.

Where to start reading

  1. The start or dev script
  2. Where Bun.serve or a framework like Elysia or Hono is started

How a request moves through the code

  1. 1bun run executes TypeScript directly without a build step.
  2. 2Bun.serve routes requests to a fetch handler or a routes object.
  3. 3Handlers use built-ins such as Bun.file and bun:sqlite rather than npm packages.

Questions to ask about the repository

  • Which Bun-only APIs does this code use, and would it run on Node?
  • How are routes defined in Bun.serve?
  • What does bunfig.toml change?

What trips people up

  • Code using Bun.* or bun: imports won't run on Node; check before assuming portability.
  • Bun's Node compatibility is broad but not complete, so native modules can behave differently.