Shipping a Dual ESM/CJS Package, Step by Step
You know what ES modules and CommonJS are. What you want is a package that works for both import and require() without breaking type checking or loading two copies of your state. This walkthrough builds one, using a hypothetical utility library called Package A, and ends with the mistakes we see most often.
Before you start: decide whether you need dual output at all
Dual publishing doubles your build output and the number of ways things can go wrong. If every consumer you care about runs a recent Node.js release or a bundler, ESM-only is a legitimate choice, and recent Node.js versions can load ES modules from require() in many cases (check the Node.js documentation for the versions you support). Go dual when you have real CommonJS consumers you cannot drop yet, and write down when you plan to stop.
The steps
- Pick the source of truth. Write source in ESM (TypeScript or plain JS). Generate CommonJS from it, never the other way round.
- Emit two folders. Build to
dist/esm/anddist/cjs/. Most bundler-style build tools can emit both formats in one run; with plaintscyou run two configs. - Mark the module type per folder. Set
"type": "module"in the rootpackage.json, then drop a tinypackage.jsoncontaining{"type": "commonjs"}intodist/cjs/. Alternatively use.mjsand.cjsextensions and skip the nested file. - Write the exports map. Conditions are matched in order, so put
typesfirst anddefaultlast. - Emit declarations for both formats. TypeScript resolves types under the same conditions, so the CommonJS entry needs its own
.d.cts(or a declaration file sitting next to the CJS output). - Lint the packed result. Run
npm packand check the tarball with tools such as publint and Are the Types Wrong?, both of which inspect the exports map rather than your source tree. - Smoke-test from outside. Install the tarball into two scratch projects, one ESM and one CommonJS, and import every public entry point.
Worked example: Package A's package.json
{
"name": "package-a",
"version": "2.1.0",
"type": "module",
"exports": {
".": {
"import": {
"types": "./dist/esm/index.d.ts",
"default": "./dist/esm/index.js"
},
"require": {
"types": "./dist/cjs/index.d.cts",
"default": "./dist/cjs/index.cjs"
}
},
"./package.json": "./package.json"
},
"main": "./dist/cjs/index.cjs",
"types": "./dist/cjs/index.d.cts",
"files": ["dist"],
"sideEffects": false
}
The top-level main and types fields are fallbacks for older tooling that ignores exports. Exporting ./package.json is a courtesy: some tools read it, and once an exports map exists, anything not listed is blocked.
The dual package hazard
The Node.js documentation describes the "dual package hazard": an application may end up loading both your ESM and your CJS build, because one dependency imports you and another requires you. If your module holds state, such as a cache, a registry of plugins or a class used with instanceof, the two copies will not see each other. Two common mitigations:
- Keep the stateful core in the CommonJS build only and make the ESM entry a thin wrapper that re-exports it.
- Avoid module-level state entirely and avoid
instanceofchecks across package boundaries; check a symbol or a brand property instead.
Common mistakes
| Mistake | Symptom | Fix |
|---|---|---|
types condition listed after default | Consumers get "could not find a declaration file" | Move types to the top of each condition block |
One .d.ts shared by both formats | Type errors only for CommonJS users | Emit separate declarations per format |
| Deep imports not listed in exports | ERR_PACKAGE_PATH_NOT_EXPORTED after upgrading | Add the subpath, or treat removing it as a breaking change |
| Adding an exports map in a minor release | Existing deep imports stop resolving | Introduce exports in a major version |
That last row catches many maintainers: adding exports to a package that had none can break people, so plan it alongside your next major. For the vocabulary, see the glossary.