Test, build, and ship

Bundle server code

Create a production Bun bundle, choose the Bun target, keep source maps, and understand what bundling does not verify.

8 minute lesson

~~~

Bun can run the TypeScript source directly. Bundling is optional for a Bun server, but it can reduce the number of files you deploy.

Create one Bun-targeted bundle:

bun build \
  --target=bun \
  --outdir=dist \
  --sourcemap=external \
  index.ts

Run the result with:

bun dist/index.js

--target=bun preserves Bun runtime APIs such as Bun.serve() and bun:sqlite. A browser target would be wrong for server code.

The source map helps error traces point back to the original TypeScript. Keep it with the deployment when your logging system can use it.

Know what the bundle contains

The bundler follows imports from index.ts and combines application code and package code. Runtime files remain separate unless you explicitly embed them.

Our notes.sqlite database should stay outside the bundle. It is changing application data, not source code.

The bundler transpiles TypeScript, but it does not replace a complete type check. Run the full verification sequence:

bun run typecheck
bun test
bun build --target=bun --outdir=dist index.ts

Then start the bundle and request /health. A successful build only proves Bun produced output. A startup check proves the output can run in the expected environment.

Lesson completed

Take this course offline

Get every free book and course as PDF and EPUB files.

Get the download library →