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

  1. Pick the source of truth. Write source in ESM (TypeScript or plain JS). Generate CommonJS from it, never the other way round.
  2. Emit two folders. Build to dist/esm/ and dist/cjs/. Most bundler-style build tools can emit both formats in one run; with plain tsc you run two configs.
  3. Mark the module type per folder. Set "type": "module" in the root package.json, then drop a tiny package.json containing {"type": "commonjs"} into dist/cjs/. Alternatively use .mjs and .cjs extensions and skip the nested file.
  4. Write the exports map. Conditions are matched in order, so put types first and default last.
  5. 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).
  6. Lint the packed result. Run npm pack and 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.
  7. 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:

Common mistakes

MistakeSymptomFix
types condition listed after defaultConsumers get "could not find a declaration file"Move types to the top of each condition block
One .d.ts shared by both formatsType errors only for CommonJS usersEmit separate declarations per format
Deep imports not listed in exportsERR_PACKAGE_PATH_NOT_EXPORTED after upgradingAdd the subpath, or treat removing it as a breaking change
Adding an exports map in a minor releaseExisting deep imports stop resolvingIntroduce 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.

Related pages