Skip to main content
Bun’s bundler implements a --compile flag for generating a standalone binary from a TypeScript or JavaScript file.
terminal
cli.ts
This bundles cli.ts into an executable you can run directly:
terminal
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-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):
terminal
To build for Linux ARM64 (for example, Graviton or Raspberry Pi):
terminal
To build for Windows x64:
terminal
To build for Windows arm64:
terminal
To build for macOS arm64:
terminal
To build for macOS x64:
terminal

Supported targets

The segments of the --target value can appear in any order, as long as they’re delimited by -.
On x64 platforms, Bun uses SIMD optimizations that require a CPU with AVX2 instructions. The -baseline build of Bun is for older CPUs without them. The Bun installer detects which version to use, but when cross-compiling you might not know the target CPU. This mostly matters on Windows x64 and Linux x64, rarely on Darwin 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
Bun inlines these constants into the binary at build time, so they cost nothing at runtime and enable dead code elimination.
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 on import 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:
terminal

Bytecode compilation

To improve startup time, enable bytecode compilation:
terminal
Using bytecode compilation, tsc starts 2x faster:
Bytecode performance comparison
Bytecode compilation moves parsing overhead for large input files from runtime to bundle time. Your app starts faster, in exchange for making the 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:
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.json and package.json loading is disabled — these are typically only needed at development time, and the bundler already uses them when compiling
  • .env and bunfig.toml loading 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 read tsconfig.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:
terminal

Act as the Bun CLI

New in Bun v1.2.16
Set the 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
Normally, running ./such-bun with arguments executes the script.
terminal
However, with the BUN_BE_BUN=1 environment variable, it acts like the bun binary:
terminal
CLI tools built on top of Bun can use this to install packages, bundle dependencies, or run other files without downloading a separate binary or installing Bun.

Full-stack executables

New in Bun v1.2.17
The --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.
To build this into a single executable:
terminal
This creates a self-contained binary that includes:
  • Your server code
  • The Bun runtime
  • All frontend assets (HTML, CSS, JavaScript)
  • Any npm packages used by your server
The result is a single file you can deploy anywhere without installing Node.js, Bun, or any dependencies:
terminal
Bun serves the frontend assets with the correct MIME types and cache headers. The HTML import is replaced with a manifest object that 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:
terminal
Then, reference the worker in your code:
index.ts
When you add multiple entrypoints to a standalone executable, each is bundled separately into the executable. We may eventually detect statically-known paths in 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 use bun:sqlite imports with bun build --compile. By default, the database is resolved relative to the current working directory of the process.
index.ts
That means if the executable is at /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 the with { type: "file" } import attribute to embed a file:
index.ts
The import returns a path string that points to the embedded file. At build time, Bun:
  1. Reads the file contents
  2. Embeds the data into the executable
  3. Replaces the import with an internal path (prefixed with /$bunfs/)
You can then read this embedded file using 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

Use static routes in Bun.serve() for efficient static file serving:
server.ts
Bun automatically handles Content-Type headers and caching for static routes.

Embedding templates

index.ts

Embedding binary files

index.ts

Embed SQLite databases

To embed a SQLite database into the compiled executable, set type: "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
Finally, compile it into a standalone executable:
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.
In the compiled executable, the embedded database is read-write, but all changes are lost when the executable exits (since it’s stored in memory).

Embed N-API Addons

You can embed .node files into executables.
index.ts
If you’re using @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 with bun build --compile, include file patterns in your build:
terminal
Then, you can reference the files in your code:
index.ts
This is a workaround, and we expect to replace it with a more direct API.

Detecting standalone mode at runtime

Use Bun.isStandaloneExecutable to check whether the current process is running from a compiled binary:
index.ts
Unlike 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
Each item in Bun.embeddedFiles is a Blob with a name property:
Use it to serve every embedded asset through 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:
terminal

Minification

To trim down the size of the executable, enable minification:
terminal
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, platform-specific options customize metadata on the generated .exe file:
terminal
Available Windows options:
  • icon - Path to .ico file for the executable icon
  • hideConsole - Disable the background terminal (for GUI apps)
  • title - Application title in file properties
  • publisher - Publisher name in file properties
  • version - Version string in file properties
  • description - Description in file properties
  • copyright - Copyright notice in file properties
These flags 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 the codesign command.
terminal
We recommend including an entitlements.plist file with JIT permissions.
info.plist
To codesign with JIT support, pass the --entitlements flag to codesign.
terminal
After codesigning, verify the executable:
terminal
Codesign support requires Bun v1.2.4 or newer.

Code splitting

Standalone executables support code splitting. Use --compile with --splitting to create an executable that loads code-split chunks at runtime.
terminal
terminal

Using plugins

Plugins work with standalone executables; use them to transform files during the build:
build.ts
Example use case - embedding environment config at build time:
cli.ts
Plugins can perform any transformation: compile YAML/TOML configs, inline SQL queries, generate type-safe API clients, or preprocess templates. See the plugin documentation.

Unsupported CLI arguments

The --compile flag does not support the following flags:
  • --outdir — use outfile instead.
  • --public-path
  • --target=node
  • --target=browser (without HTML entrypoints — see Standalone HTML for --compile --target=browser with .html files)
  • --no-bundle - Bun always bundles everything into the executable.

API reference

The compile option in Bun.build() accepts three forms:
types
Usage forms:

Supported targets

Bun.Build.CompileTarget

Complete example

build.ts