Node "Dynamic require of X is not supported" in Bundled ESM in CI - Fix the Bundle
When a bundler emits ESM, runtime require() is not available, so any require call that survives bundling throws "Dynamic require of X is not supported".
What this error means
A bundled output run in CI throws Error: Dynamic require of "<name>" is not supported, from a dependency that still calls require() at runtime under the ESM bundle.
Error: Dynamic require of "fs" is not supported
at file:///home/runner/work/app/app/dist/index.mjs:12:9
at node_modules/some-cjs-dep/index.jsCommon causes
A CommonJS dependency using runtime require in an ESM bundle
The bundler produced ESM, but a bundled CommonJS module calls require() at runtime, which the ESM output cannot satisfy.
Conditional or dynamic require inlined into the bundle
A require behind a condition was inlined instead of externalized, so it runs in the ESM context.
How to fix it
Mark the dependency external
- Keep the CommonJS dependency external so it loads from node_modules at runtime.
- Ship node_modules alongside the bundle.
// esbuild
external: ['some-cjs-dep']Target a CommonJS bundle format
- Bundle to CommonJS so runtime require is available.
- Re-run the output with node.
esbuild src/index.ts --bundle --platform=node --format=cjs --outfile=dist/index.jsHow to prevent it
- Choose the bundle format that matches how dependencies load, externalize Node built-ins and CommonJS-only packages, and test the bundled artifact in CI before relying on it.