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 binarybun.lockbbunfig.toml, andbun runorbun testin scriptsBun.serve,Bun.fileand imports frombun:sqliteorbun: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.servedirectly. - *.test.ts
- Tests using
bun:test.
Conventions, not rules. Real projects rename and move things, so confirm against the repository.
Where to start reading
- The
startordevscript - Where
Bun.serveor a framework like Elysia or Hono is started
How a request moves through the code
bun runexecutes TypeScript directly without a build step.Bun.serveroutes requests to afetchhandler or aroutesobject.- Handlers use built-ins such as
Bun.fileandbun:sqliterather 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.tomlchange?
What trips people up
- Code using
Bun.*orbun: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.