--compile flag for generating a standalone binary from a TypeScript or JavaScript file.
cli.ts into an executable that can be executed directly:
terminal
Cross-compile to other platforms
The--target flag lets you compile your standalone executable for a different operating system, architecture, or version of Bun than the machine you’re running bun build on.
To build for Linux x64 (most servers):
terminal
terminal
terminal
terminal
terminal
Supported targets
The order of the--target flag does not matter, as long as they’re delimited by a -.
| —target | Operating System | Architecture | Modern | Baseline | Libc | 
|---|---|---|---|---|---|
| bun-linux-x64 | Linux | x64 | ✅ | ✅ | glibc | 
| bun-linux-arm64 | Linux | arm64 | ✅ | N/A | glibc | 
| bun-windows-x64 | Windows | x64 | ✅ | ✅ | - | 
| Windows | arm64 | ❌ | ❌ | - | |
| bun-darwin-x64 | macOS | x64 | ✅ | ✅ | - | 
| bun-darwin-arm64 | macOS | arm64 | ✅ | N/A | - | 
| bun-linux-x64-musl | Linux | x64 | ✅ | ✅ | musl | 
| bun-linux-arm64-musl | Linux | arm64 | ✅ | N/A | musl | 
On x64 platforms, Bun uses SIMD optimizations which require a modern CPU supporting AVX2
instructions. The 
-baseline build of Bun is for older CPUs that don’t support these
optimizations. Normally, when you install Bun we automatically detect which version to use but
this can be harder to do when cross-compiling since you might not know the target CPU. You usually
don’t need to worry about it on Darwin x64, but it is relevant for Windows x64 and Linux x64. If
you or your users see "Illegal instruction" errors, you might need to use the baseline version.Build-time constants
Use the--define flag to inject build-time constants into your executable, such as version numbers, build timestamps, or configuration values:
terminal
For comprehensive examples and advanced patterns, see the Build-time constants
guide.
Deploying to production
Compiled executables reduce memory usage and improve Bun’s start time. Normally, Bun reads and transpiles JavaScript and TypeScript files onimport and require. This is part of what makes so much of Bun “just work”, but it’s not free. It costs time and memory to read files from disk, resolve file paths, parse, transpile, and print source code.
With compiled executables, you can move that cost from runtime to build-time.
When deploying to production, we recommend the following:
terminal
Bytecode compilation
To improve startup time, enable bytecode compilation:terminal
tsc starts 2x faster:
bun build command a little slower. It doesn’t obscure source code.
Experimental: Bytecode compilation is an experimental feature introduced in Bun v1.1.30. Only
cjs format is supported (which means no top-level-await). Let us know if you run into any
issues!What do these flags do?
The--minify argument optimizes the size of the transpiled output code. If you have a large application, this can save megabytes of space. For smaller applications, it might still improve start time a little.
The --sourcemap argument embeds a sourcemap compressed with zstd, so that errors & stacktraces point to their original locations instead of the transpiled location. Bun will automatically decompress & resolve the sourcemap when an error occurs.
The --bytecode argument enables bytecode compilation. Every time you run JavaScript code in Bun, JavaScriptCore (the engine) will compile your source code into bytecode. We can move this parsing work from runtime to bundle time, saving you startup time.
Embedding runtime arguments
--compile-exec-argv="args" - Embed runtime arguments that are available via process.execArgv:
terminal
Act as the Bun CLI
New in Bun v1.2.16
bun CLI itself by setting the BUN_BE_BUN=1 environment variable. When this variable is set, the executable will ignore its bundled entrypoint and instead expose all the features of Bun’s CLI.
For example, consider an executable compiled from a simple script:
terminal
./such-bun with arguments would execute the script.
terminal
BUN_BE_BUN=1 environment variable, it acts just like the bun binary:
terminal
Full-stack executables
New in Bun v1.2.17
--compile flag can create standalone executables that contain both server and client code, making it ideal for full-stack applications. When you import an HTML file in your server code, Bun automatically bundles all frontend assets (JavaScript, CSS, etc.) and embeds them into the executable. When Bun sees the HTML import on the server, it kicks off a frontend build process to bundle JavaScript, CSS, and other assets.
terminal
- Your server code
- The Bun runtime
- All frontend assets (HTML, CSS, JavaScript)
- Any npm packages used by your server
terminal
Bun.serve uses to efficiently serve pre-bundled assets.
For more details on building full-stack applications with Bun, see the full-stack guide.
Worker
To use workers in a standalone executable, add the worker’s entrypoint to the CLI arguments:terminal
new Worker(path) and then bundle those into the executable, but for now, you’ll need to add it to the shell command manually like the above example.
If you use a relative path to a file not included in the standalone executable, it will attempt to load that path from disk relative to the current working directory of the process (and then error if it doesn’t exist).
SQLite
You can usebun:sqlite imports with bun build --compile.
By default, the database is resolved relative to the current working directory of the process.
/usr/bin/hello, the user’s terminal is located at /home/me/Desktop, it will look for /home/me/Desktop/my.db.
terminal
Embed assets & files
Standalone executables support embedding files. To embed files into an executable withbun build --compile, import the file in your code.
Bun.file’s functions or the Node.js fs.readFile function (in "node:fs").
For example, to read the contents of the embedded file:
Embed SQLite databases
If your application wants to embed a SQLite database, settype: "sqlite" in the import attribute and the embed attribute to "true".
Embed N-API Addons
As of Bun v1.0.23, you can embed.node files into executables.
@mapbox/node-pre-gyp or other similar tools, you’ll need to make sure the .node file is directly required or it won’t bundle correctly.
Embed directories
To embed a directory withbun build --compile, use a shell glob in your bun build command:
terminal
Listing embedded files
To get a list of all embedded files, useBun.embeddedFiles:
Bun.embeddedFiles returns an array of Blob objects which you can use to get the size, contents, and other properties of the files.
.ts and .js files.
Content hash
By default, embedded files have a content hash appended to their name. This is useful for situations where you want to serve the file from a URL or CDN and have fewer cache invalidation issues. But sometimes, this is unexpected and you might want the original name instead: To disable the content hash, pass--asset-naming to bun build --compile like this:
terminal
Minification
To trim down the size of the executable a little, pass--minify to bun build --compile. This uses Bun’s minifier to reduce the code size. Overall though, Bun’s binary is still way too big and we need to make it smaller.
Windows-specific flags
When compiling a standalone executable on Windows, there are two platform-specific options that can be used to customize metadata on the generated.exe file:
- --windows-icon=path/to/icon.icoto customize the executable file icon.
- --windows-hide-consoleto disable the background terminal, which can be used for applications that do not need a TTY.
These flags currently cannot be used when cross-compiling because they depend on Windows APIs.
Code signing on macOS
To codesign a standalone executable on macOS (which fixes Gatekeeper warnings), use thecodesign command.
terminal
entitlements.plist file with JIT permissions.
info.plist
--entitlements flag to codesign.
terminal
terminal
Codesign support requires Bun v1.2.4 or newer.
Unsupported CLI arguments
Currently, the--compile flag can only accept a single entrypoint at a time and does not support the following flags:
- --outdir— use- outfileinstead.
- --splitting
- --public-path
- --target=nodeor- --target=browser
- --no-bundle- we always bundle everything into the executable.