Dual ESM and CommonJS Packages: A Position Paper
For years the safe answer to "ESM or CommonJS?" was "both". Shipping two builds lets every consumer import your package, but it doubles the surface you test, introduces subtle runtime bugs and keeps an old module system alive. This paper looks at whether the dual-build default still holds and proposes a way to decide per package.
Problem
JavaScript has two module systems. CommonJS (require, module.exports) is Node.js's original format. ES modules (import, export) are the language standard and the browser's native format. Node.js supports both, and for a long time a CommonJS file could not synchronously require() an ES module, which forced library authors to provide a CommonJS build if they wanted CommonJS users. Newer Node.js release lines have added support for requiring ES modules in many cases, which changes the calculation, but older runtimes and some tools still matter.
Background: the costs of going dual
- The dual-package hazard. If one part of an application imports your ESM build and another requires your CJS build, two separate copies of your module can load. Singletons,
instanceofchecks and module-level caches then misbehave. The Node.js documentation describes this hazard explicitly. - Build complexity. You need two outputs, two sets of type declarations in some setups, and an
exportsmap that routes each condition correctly. - Testing surface. Every test should, in principle, run against both builds. In practice most projects test one and hope.
- Size. The published tarball roughly doubles.
A typical dual exports map looks like this:
{
"type": "module",
"exports": {
".": {
"import": { "types": "./dist/index.d.ts", "default": "./dist/index.js" },
"require": { "types": "./dist/index.d.cts", "default": "./dist/index.cjs" }
}
}
}
Options
- Dual build. Maximum compatibility, highest maintenance cost, hazard risk if your module holds state.
- ESM-only. One build, standard syntax, smaller package. Users on older runtimes or CommonJS-only tooling must use dynamic
import()or stay on your previous major. - CJS-only. Works everywhere Node.js does and can be imported from ESM, but loses static analysis benefits such as reliable tree-shaking and is increasingly out of step with the ecosystem.
- ESM source with a thin CJS wrapper. The CJS entry re-exports the ESM build, which avoids two copies of state, at the cost of relying on runtime support for loading ESM from CJS.
Recommendation
Decide from your users, not from fashion. If your package is new, has no state that would break if loaded twice, and your users run current Node.js releases and modern bundlers, ESM-only in a new major version is a reasonable default and the simplest to maintain. If you have a large CommonJS user base, a dual build remains defensible, but keep module-level state out of the shared code or isolate it in one file both builds load. In either case:
- Make the switch in a major version and say so in the changelog.
- State the minimum Node.js version in the
enginesfield. - Verify the published package with a tool that checks
exportsresolution and type declarations, such as "Are the Types Wrong?".
The step-by-step build is covered in our dual ESM/CJS tutorial, and the exports field itself in the package.json exports how-to.