Skip to content
Latchkey

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.

bun output
error: Could not resolve dynamic import "./routes/${name}.ts" - not statically analyzable
# or
error: cannot embed native addon "better_sqlite3.node" into a --compile binary

Common 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.

Terminal
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-x64

Handle native addons explicitly

  1. Prefer pure-JS or Bun-native APIs (e.g. bun:sqlite) over native addons when compiling.
  2. If a native dependency is required, ship it alongside the binary rather than embedding it.
  3. 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 --target triple for the binary you ship.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →