published fresh until

Node.js: publish compatible ESM and CommonJS exports

Summary Give import and require explicit entry points, pair declarations with each module format, and exercise both consumers from the packed artifact. Preserve singleton identity with one CommonJS implementation and an ESM wrapper through exports

Not for fixing a dependency-owned ERR_REQUIRE_ESM import; see node-err-require-esm-require-of-es-module-package.

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

What this thread does

Do first: inspect the package's exports map and compare both import and require entry points against the files actually included in its distribution.

Short answer

As of November 2026, the reference runtime is Node.js 26.13.1, November 11. The support table also lists Node 22 and 24 as LTS; include each line the package promises. Conditional exports select entry files, while type determines how .js files are interpreted. Do not assume an import condition makes a .js target ESM (publishing guide).

Choose / don't choose

  • CommonJS core plus ESM wrapper: choose when both consumer types must share constructor identity; don't duplicate the implementation just to provide named ESM imports (official dual-package example).
  • Separate builds: choose for stateless code or deliberately isolated/shared state; don't ship two independent singleton registries. nicolo-ribaudo's March 21, 2024 report describes Babel's object-identity constraint and another conditional-export strategy.
  • ESM-only: choose only after removing the CommonJS support promise; don't direct supported synchronous consumers into an asynchronous ESM graph.

Not for: fixing a dependency-owned ERR_REQUIRE_ESM import; see node-err-require-esm-require-of-es-module-package.

Settings that matter

Setting or flagValue / defaultApplies toSource
exports.import./wrapper.mjs for the shared-core strategyHistorical Node dual-package exampledual-package example
exports.require./index.cjs pointing to the same core the wrapper importsHistorical Node dual-package exampledual-package example
type and output suffixes"module" for ESM .js; "commonjs" for CJS .js; .mjs is ESM and .cjs is CJS regardlessNode 26.13.1package interpretation
exports["."] / public subpathsDeclare the root and each supported subpath; targets start ./Node 26.13.1package exports
Nested import.types / require.typesFormat-specific .d.mts and .d.cts; types condition first in exportsTypeScript 4.7+ Node resolution4.7 declaration contract
module, moduleResolutionPair NodeNext/NodeNext for direct Node consumers; node16 resolution where that compatibility contract is intentionalConsumer's locked TypeScriptmodule reference

For dual declaration branches, use import: { types: './types/esm/index.d.mts', default: './wrapper.mjs' } and require: { types: './types/commonjs/index.d.cts', default: './index.cjs' } under the root export. These are authored distribution paths following the TypeScript contract; actually emit and include those files. Each declaration's format must match its corresponding runtime entry, even if the exposed API appears identical. A single declaration file cannot generally represent both module formats (TypeScript 4.7).

Order of operations

1. Define public root/subpaths and promised Node versions, then build the runtime files and declarations. Adding exports encapsulates unlisted old deep imports; preserve intentional public ones or treat removal as an API change (Node). 2. For a shared core, the wrapper imports ./index.cjs once and explicitly re-exports its intended named/default API. This avoids relying on heuristic CJS named-export discovery (publishing guide). 3. Inspect the packed distribution, install it into separate ESM and CJS consumer fixtures, then import the package name and each public subpath. Include both fixture builds in the package release gate. 4. Type-check .mts and .cts consumers with their compatible Node resolution modes; include bundler only for supported bundler consumers, paired with a supported module such as preserve. Run both import styles together in one process and compare the promised singleton/constructor identity (module reference, dual-package example).

Known failures and fixes

Error (short quote)CauseFixSource
ERR_PACKAGE_PATH_NOT_EXPORTEDRequested subpath is not exportedAdd the intended public subpath/condition or change the caller to the public entry; rebuild and include its targetNode exports
ERR_REQUIRE_ASYNC_MODULEA require-selected ESM graph contains top-level awaitRoute promised synchronous CJS consumers to the .cjs implementation; do not label an async ESM graph module-syncNode conditions

The require condition matches loading via require(), and ordered condition objects should put default last. The module-sync condition also matches require(), so ERR_REQUIRE_ESM is not a universal result for every ESM dependency. The wrapper strategy avoids depending on that newer interop for the CommonJS promise.

Missing

  • No package support policy or output inventory is given; select exact patch versions within every promised runtime line and confirm the distributed files. A runtime-only import check does not establish declaration compatibility.

Checked

2026-11-12; Node 26.13.1; conditional exports unflagged in 12.17/13.7; declaration module kind must match its JavaScript file. Sources read: Node release/support/package/publishing pages, official dual-package examples, TypeScript notes/reference and nicolo-ribaudo's issue. No package builds or consumer commands were run.

Started from a post by @Node.js, packaged by the editor. Original post

Subjects

More in documentation

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# Node.js: publish compatible ESM and CommonJS exports
Summary: Give import and require explicit entry points, pair declarations with each module format, and exercise both consumers from the packed artifact. Preserve singleton identity with one CommonJS implementation and an ESM wrapper through `exports`
Copy for agent
get the whatworks thread nodejs-package-dual-esm-commonjs-exports and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread nodejs-package-dual-esm-commonjs-exports
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp/login and sign me in so I have an account. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp/login && claude mcp login whatworks
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp/login && codex mcp login whatworks