Skip to main content
Bun’s bundler supports the --compile flag for generating standalone binary executables from TypeScript or JavaScript files.
cli.ts
This bundles cli.ts into an executable binary that can be executed directly:
All imported files and packages are bundled into the executable, along with a copy of the Bun runtime. All built-in Bun and Node.js APIs are supported.

Cross-platform compilation

The --target flag allows you to compile standalone executables for a different operating system, architecture, or Bun version than the machine running bun build. Build for Linux x64 (most servers):
Build for Linux ARM64 (like Graviton or Raspberry Pi):
Build for Windows x64:
Build for Windows ARM64:
Build for macOS ARM64:
Build for macOS x64:

Available targets

The following targets are available:

Embedding files

You can embed files into the executable by importing them in your code. The bundler will automatically include them.
These files are extracted to a temporary directory at runtime and can be accessed via their import paths.

Configuration options

minify

You can minify the bundled code:

sourcemap

Generate source maps for debugging:
This creates a .map file alongside your executable.

splitting

Code splitting is not supported when using --compile since the output is a single executable.

Environment variables

By default, executables do NOT load .env files. You can change this behavior:
Or inline environment variables at build time:

Command-line arguments

Your executable receives command-line arguments via process.argv:
cli.ts

Size considerations

Standalone executables include the Bun runtime, so they start at around 50-90 MB depending on the target platform. This size includes:
  • The Bun JavaScript runtime
  • JavaScriptCore engine
  • Your bundled code and dependencies
  • Any embedded assets
You can reduce size with:
  • --minify to compress your code
  • --external to exclude large dependencies
  • Tree shaking (automatic) to remove unused code

Deployment

Standalone executables are single-file deployables with no dependencies:
  1. Build for your target platform
  2. Copy the executable to your server
  3. Set executable permissions (Unix): chmod +x myapp
  4. Run: ./myapp
No need to install Bun or Node.js on the deployment target.

Troubleshooting

Large binary size

If your executable is larger than expected:
  • Check for unintended imports of large dependencies
  • Use --external for dependencies you want to load at runtime
  • Enable --minify to compress code

Runtime errors

If your executable fails at runtime:
  • Test your code with bun run before compiling
  • Check that all file paths are relative or properly configured
  • Verify embedded assets are imported correctly

Cross-compilation issues

When building for a different platform:
  • Ensure you’re using the correct --target value
  • Test on the target platform before deploying
  • Some native modules may not work when cross-compiled

Examples

Simple CLI tool

cli.ts

HTTP server

server.ts

With embedded assets

app.ts