--compile flag for generating standalone binary executables from TypeScript or JavaScript files.
- CLI
- JavaScript
cli.ts
cli.ts into an executable binary that can be executed directly:
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):
- CLI
- JavaScript
- CLI
- JavaScript
- CLI
- JavaScript
- CLI
- JavaScript
- CLI
- JavaScript
- CLI
- JavaScript
Available targets
The following targets are available:| Target | OS | Architecture | Profile |
|---|---|---|---|
bun-linux-x64 | Linux | x64 | default |
bun-linux-x64-baseline | Linux | x64 | baseline (pre-2013) |
bun-linux-x64-modern | Linux | x64 | modern (2013+) |
bun-linux-arm64 | Linux | ARM64 | default |
bun-windows-x64 | Windows | x64 | default |
bun-windows-x64-baseline | Windows | x64 | baseline (pre-2013) |
bun-windows-x64-modern | Windows | x64 | modern (2013+) |
bun-windows-arm64 | Windows | ARM64 | default |
bun-darwin-arm64 | macOS | ARM64 | default |
bun-darwin-x64 | macOS | x64 | default |
Embedding files
You can embed files into the executable by importing them in your code. The bundler will automatically include them.Configuration options
minify
You can minify the bundled code:sourcemap
Generate source maps for debugging:.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:
Command-line arguments
Your executable receives command-line arguments viaprocess.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
--minifyto compress your code--externalto exclude large dependencies- Tree shaking (automatic) to remove unused code
Deployment
Standalone executables are single-file deployables with no dependencies:- Build for your target platform
- Copy the executable to your server
- Set executable permissions (Unix):
chmod +x myapp - Run:
./myapp
Troubleshooting
Large binary size
If your executable is larger than expected:- Check for unintended imports of large dependencies
- Use
--externalfor dependencies you want to load at runtime - Enable
--minifyto compress code
Runtime errors
If your executable fails at runtime:- Test your code with
bun runbefore 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
--targetvalue - 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