--compile flag for generating a standalone binary from a TypeScript or JavaScript file.
- CLI
- JavaScript
terminal
cli.ts
cli.ts into an executable you can run directly:
terminal
Cross-compile to other platforms
Use the--target flag to 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):
- CLI
- JavaScript
terminal
- CLI
- JavaScript
terminal
- CLI
- JavaScript
terminal
- CLI
- JavaScript
terminal
- CLI
- JavaScript
terminal
- CLI
- JavaScript
terminal
Supported targets
The segments of the--target value can appear in any order, as long as they’re delimited by -.
Build-time constants
Use the--define flag to inject build-time constants into your executable, such as version numbers, build timestamps, or configuration values:
- CLI
- JavaScript
terminal
For more examples and 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: reading files from disk, resolving paths, parsing, transpiling, and printing source code costs time and memory.
Compiled executables move that cost from runtime to build time.
When deploying to production, we recommend the following:
- CLI
- JavaScript
terminal
Bytecode compilation
To improve startup time, enable bytecode compilation:- CLI
- JavaScript
terminal
tsc starts 2x faster:
bun build command a little slower. It doesn’t obscure source code.
Bytecode compilation supports both
cjs and esm formats when used with --compile.What do these flags do?
The--minify argument reduces the size of the transpiled output code. For 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 decompresses & resolves the sourcemap automatically when an error occurs.
The --bytecode argument enables bytecode compilation. Every time you run JavaScript code in Bun, JavaScriptCore (the engine) compiles your source code into bytecode. --bytecode moves that parsing work from runtime to bundle time, which shortens startup.
Embedding runtime arguments
--compile-exec-argv="args" - Embed runtime arguments, available at runtime in process.execArgv:
- CLI
- JavaScript
terminal
app.ts
Runtime arguments via BUN_OPTIONS
Standalone executables read the BUN_OPTIONS environment variable, so you can pass runtime flags without recompiling:
terminal
Automatic config loading
Standalone executables can automatically load configuration files from the directory where they are run. By default:tsconfig.jsonandpackage.jsonloading is disabled — these are typically only needed at development time, and the bundler already uses them when compiling.envandbunfig.tomlloading is enabled — these often contain runtime configuration that may vary per deployment
In a future version of Bun,
.env and bunfig.toml may also be disabled by default for more deterministic behavior.Enabling config loading at runtime
If your executable needs to readtsconfig.json or package.json at runtime, opt in with these flags:
terminal
Disabling config loading at runtime
To disable.env or bunfig.toml loading for deterministic execution:
- CLI
- JavaScript
terminal
Act as the Bun CLI
New in Bun v1.2.16
BUN_BE_BUN=1 environment variable to run a standalone executable as if it were the bun CLI itself. The executable ignores its bundled entrypoint and exposes the full bun CLI instead.
For example, consider an executable compiled from this script:
terminal
./such-bun with arguments executes the script.
terminal
BUN_BE_BUN=1 environment variable, it acts like the bun binary:
terminal
Full-stack executables
New in Bun v1.2.17
--compile flag can create a standalone executable that contains both server and client code, which suits full-stack applications. When you import an HTML file in your server code, Bun bundles the frontend assets (JavaScript, CSS, and so on) and embeds them into the executable.
- CLI
- JavaScript
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 serve the pre-bundled assets.
For more on building full-stack applications, see the full-stack guide.
Worker
To use workers in a standalone executable, add the worker’s entrypoint to the build:- CLI
- JavaScript
terminal
index.ts
new Worker(path) and bundle them automatically, but for now you need to list the worker file as an entrypoint, as in the earlier example.
If you use a relative path to a file not included in the standalone executable, Bun loads that path from disk relative to the process’s current working directory, and errors 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.
index.ts
/usr/bin/hello and the user’s terminal is in /home/me/Desktop, Bun looks for /home/me/Desktop/my.db.
terminal
Embed assets & files
Standalone executables can embed files directly into the binary, so a single executable can ship images, JSON configs, templates, or any other assets your application needs.How it works
Use thewith { type: "file" } import attribute to embed a file:
index.ts
- Reads the file contents
- Embeds the data into the executable
- Replaces the import with an internal path (prefixed with
/$bunfs/)
Bun.file() or Node.js fs APIs.
Reading embedded files with Bun.file()
Bun.file() is the recommended way to read embedded files:
index.ts
Reading embedded files with Node.js fs
Embedded files work with the Node.js file system APIs:index.ts
Practical examples
Embedding a JSON config file
index.ts
Serving static assets in an HTTP server
Usestatic routes in Bun.serve() for efficient static file serving:
server.ts
Embedding templates
index.ts
Embedding binary files
index.ts
Embed SQLite databases
To embed a SQLite database into the compiled executable, settype: "sqlite" in the import attribute and the embed attribute to "true".
The database file must already exist on disk. Then, import it in your code:
index.ts
terminal
The database file must exist on disk when you run
bun build --compile. The embed: "true" attribute tells the
bundler to include the database contents inside the compiled executable. When running normally with bun run, the
database file is loaded from disk just like a regular SQLite import.Embed N-API Addons
You can embed.node files into executables.
index.ts
@mapbox/node-pre-gyp or similar tools, the .node file must be required directly or it won’t bundle correctly.
Embed directories
To embed a directory withbun build --compile, include file patterns in your build:
- CLI
- JavaScript
terminal
index.ts
Detecting standalone mode at runtime
UseBun.isStandaloneExecutable to check whether the current process is running from a compiled binary:
index.ts
Bun.embeddedFiles.length > 0, this check does not allocate Blob objects for each embedded file, so it is safe to call at startup in binaries that embed large assets.
Listing embedded files
Bun.embeddedFiles exposes all embedded files as Blob objects:
index.ts
Bun.embeddedFiles is a Blob with a name property:
static routes:
server.ts
Bun.embeddedFiles excludes bundled source code (.ts, .js, etc.) to help protect your application’s source.Content hash
By default, embedded files have a content hash appended to their name, which helps with cache invalidation when you serve them from a URL or CDN. To keep the original name instead, configure asset naming:- CLI
- JavaScript
terminal
Minification
To trim down the size of the executable, enable minification:- CLI
- JavaScript
terminal
Windows-specific flags
When compiling a standalone executable on Windows, platform-specific options customize metadata on the generated.exe file:
- CLI
- JavaScript
terminal
icon- Path to.icofile for the executable iconhideConsole- Disable the background terminal (for GUI apps)title- Application title in file propertiespublisher- Publisher name in file propertiesversion- Version string in file propertiesdescription- Description in file propertiescopyright- Copyright notice in file properties
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
Code splitting
Standalone executables support code splitting. Use--compile with --splitting to create an executable that loads code-split chunks at runtime.
- CLI
- JavaScript
terminal
terminal
Using plugins
Plugins work with standalone executables; use them to transform files during the build:build.ts
cli.ts
Unsupported CLI arguments
The--compile flag does not support the following flags:
--outdir— useoutfileinstead.--public-path--target=node--target=browser(without HTML entrypoints — see Standalone HTML for--compile --target=browserwith.htmlfiles)--no-bundle- Bun always bundles everything into the executable.
API reference
Thecompile option in Bun.build() accepts three forms:
types
Supported targets
Bun.Build.CompileTarget
Complete example
build.ts