Dynamic import of CJS entry point fails with --splitting
Author: aliiCreated Jan 15, 2026Updated Aug 28, 2026
When using --splitting, a dynamic import() of a CommonJS module that is also an entry point fails at runtime because the default export is not properly unwrapped.
Reproduction
# alistair @ Alistair-Smith-MacBook in ~/code/testing2 [11:31:43]
$ bat main.js impl.js
─────┬─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
│ File: main.js
─────┼─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
1 │ import("./impl.js").then(mod => console.log(mod.foo()));
─────┴─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
─────┬─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
│ File: impl.js
─────┼─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
1 │ exports.foo = () => "success";
─────┴─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
# alistair @ Alistair-Smith-MacBook in ~/code/testing2 [11:31:45]
$ bunx esbuild main.js impl.js --bundle --format=esm --splitting --outdir=out
out/impl.js 351b
out/main.js 70b
⚡ Done in 2ms
# alistair @ Alistair-Smith-MacBook in ~/code/testing2 [11:31:47]
$ bun out/main.js
1 | // main.js
2 | import("./impl.js").then((mod) => console.log(mod.foo()));
^
TypeError: mod.foo is not a function. (In 'mod.foo()', 'mod.foo' is undefined)
at <anonymous> (/Users/alistair/code/testing2/out/main.js:2:51)
Bun v1.3.7-canary.1+021993641 (macOS arm64)
# alistair @ Alistair-Smith-MacBook in ~/code/testing2 [11:31:49] C:1
$ bun main.js
successAnalysis
The generated impl.js chunk exports:
export default require_impl();Where require_impl() returns { __esModule: true, getValue: fn }.
When the dynamic import("./impl.js") resolves, it returns:
{ default: { __esModule: true, getValue: fn } }The code tries to access mod.getValue, but the function is nested at mod.default.getValue.
For dynamic imports of CJS entry point chunks, perhaps the import should be wrapped to unwrap the default? E.g.
const mod = await import("./impl.js").then((m) => __toESM(m.default));This is similar to how static imports of CJS modules are wrapped with __toESM(require_foo()).
Source: evanw/esbuild