bun build: Bundle for Production
bun build bundles your entrypoints into optimized output using Bun's fast built-in bundler.
bun build is the bundler half of the toolkit. It targets browser, node, or bun runtimes, can minify and code-split, and with --compile can produce a single self-contained executable.
What it does
bun build takes one or more entrypoints and produces bundled output, resolving imports, transpiling TS/JSX, and applying minification and tree-shaking. The --target flag selects the platform (browser, node, bun), and --compile bundles the runtime in to emit a standalone binary.
Common usage
bun build ./src/index.ts --outdir ./dist
bun build ./src/index.ts --outdir ./dist --target browser --minify
bun build ./src/index.ts --outdir ./dist --splitting --format esm
bun build ./cli.ts --compile --outfile mycli # standalone executableOptions
| Flag | What it does |
|---|---|
| --outdir <dir> | Directory for bundled output |
| --outfile <file> | Single output file (or executable with --compile) |
| --target <browser|node|bun> | Platform the bundle runs on |
| --minify | Minify the output |
| --splitting | Enable code splitting (ESM only) |
| --compile | Produce a standalone executable with the runtime baked in |
| --sourcemap <type> | Emit source maps (linked, inline, external) |
In CI
Set --target explicitly; the default is browser and a Node service bundled as browser will fail on built-in modules like fs. Cache the bundler is unnecessary, but cache ~/.bun/install/cache so the install before bun build is fast. For --compile, build on the same OS/arch you deploy to, since the executable is platform-specific.
Common errors in CI
"error: Could not resolve <module>" means an import path is wrong or a Node built-in is being bundled for the browser target; switch --target node/bun or mark it external. "ModuleNotFound" during --compile usually means a dynamic require Bun cannot statically trace. A bundle that runs locally but not in CI often used a node-only API under the default browser target.