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.
node
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 config
// esbuild
external: ['some-cjs-dep']Target a CommonJS bundle format
- Bundle to CommonJS so runtime require is available.
- Re-run the output with node.
Terminal
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.
Related guides
Node ERR_REQUIRE_ESM "require() of ES Module" in CI - Fix the InteropFix the Node.js ERR_REQUIRE_ESM error in CI by switching to a dynamic import, pinning a CommonJS-compatible v…
Node "Cannot use import statement outside a module" in CI - Fix ItFix the Node.js "SyntaxError: Cannot use import statement outside a module" in CI by telling Node to treat th…
Node ERR_PACKAGE_PATH_NOT_EXPORTED in CI - Fix the Exports Map SubpathFix the Node.js ERR_PACKAGE_PATH_NOT_EXPORTED error in CI by importing a subpath the package exports map expo…