Bun "bun build --compile" Fails in CI
bun build --compile produces a standalone executable. It failed because a dynamic import could not be bundled, a native addon cannot be embedded, or the requested --target is not supported.
What this error means
bun build --compile errors during bundling, or the produced binary fails at runtime with a missing module. Dynamic imports, native .node addons, and cross-target builds are the common causes.
error: Could not resolve dynamic import "./routes/${name}.ts" - not statically analyzable
# or
error: cannot embed native addon "better_sqlite3.node" into a --compile binaryCommon causes
Untraceable dynamic import
A computed dynamic import is not statically analyzable, so the bundler cannot include it in the standalone binary.
Native addon or unsupported target
A native .node addon cannot always be embedded, and an unsupported --target triple fails the compile.
How to fix it
Make imports static and pick a supported target
Replace computed dynamic imports with static ones and pass a valid target.
bun build ./src/index.ts --compile --target=bun-linux-x64 --outfile app
# valid targets: bun-linux-x64, bun-linux-arm64, bun-darwin-x64, bun-darwin-arm64, bun-windows-x64Handle native addons explicitly
- Prefer pure-JS or Bun-native APIs (e.g.
bun:sqlite) over native addons when compiling. - If a native dependency is required, ship it alongside the binary rather than embedding it.
- Externalize modules that cannot be bundled and resolve them at runtime.
How to prevent it
- Avoid computed dynamic imports in code you compile.
- Prefer Bun-native APIs over native addons for standalone binaries.
- Use a supported
--targettriple for the binary you ship.