Compiler

Turn a TypeScript or JavaScript program into one executable file

Bun turns a TypeScript or JavaScript program into one executable file. The file runs on a machine that doesn't have Bun installed.

cli.ts
console.log("Hello world!");
terminal
bun build --compile ./cli.ts --outfile mycli
./mycli
Hello world!

Two ways to compile#

bun build --compilebun compile
The executable holdsYour bundled JavaScript and a copy of BunYour program as machine code and Bun without its JIT
Your code runsIn the interpreter and the JIT, like bun runAs machine code. The executable has no JIT
TypeScript typesRemoved, never checkedChecked, then used. A type error stops the build
Builds forOther platforms too, with --targetOnly the platform that runs the build
StatusStableExperimental

bun compile is short for bun build --compile --aot, so the two take the same flags.

bun build --compile runs everything that bun run runs. With bun compile, servers start faster and use less memory, and a few things differ from bun run.

bun build --compile#

  • Single-file executable: cross-compile, embed files, set build-time constants, sign the executable on macOS and set its icon on Windows.
  • Bytecode caching: compile your JavaScript to bytecode at build time, so that the executable starts faster.
  • Standalone HTML: with --target=browser, --compile writes one self-contained .html file.

bun compile#