Skip to content
Latchkey

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

Common 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

  1. Keep the CommonJS dependency external so it loads from node_modules at runtime.
  2. Ship node_modules alongside the bundle.
esbuild config
// esbuild
external: ['some-cjs-dep']

Target a CommonJS bundle format

  1. Bundle to CommonJS so runtime require is available.
  2. Re-run the output with node.
Terminal
esbuild src/index.ts --bundle --platform=node --format=cjs --outfile=dist/index.js

How 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

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