Sub topic 9
Shipping ESM and CommonJS Together, with Matching Types
Assume you know TypeScript syntax, npm, package.json, and basic import and export syntax. This chapter focuses on packages that support both modern ESM and older CommonJS consumers.
Why support two module formats?
ECMAScript modules, or ESM, use import and export. CommonJS, or CJS, uses require and module.exports.
ESM works well with modern Node.js, browsers, and bundlers. CommonJS still appears in older Node.js applications and tools. A dual package provides one entry point for each format:
| Consumer code | Package condition | File |
|---|---|---|
| `import { slugify } from "@acme/slugify"` | `import` | `dist/index.js` |
| `const { slugify } = require("@acme/slugify")` | `require` | `dist/index.cjs` |
Supporting both formats increases compatibility, but it also increases build and testing work. You must ensure that both files expose the same API and that TypeScript finds the correct declarations for each one.
Match each format with its declarations
A declaration file contains types without runtime code. TypeScript uses it to check a consumer's code.
In an ESM package, index.d.ts is interpreted as an ESM declaration when the package has "type": "module". A CommonJS declaration should use the .d.cts extension. The extension tells TypeScript that the declaration belongs to CommonJS.
A dual package can use this structure:
dist/
index.js
index.cjs
index.d.ts
index.d.ctsThe ESM declaration and the CommonJS declaration should describe the same public functions and types. A difference between them can cause confusing errors that depend on how the consumer imports the package.
Configure conditional exports
The exports field selects a file based on the consumer's module system. Put the types condition before default inside each branch. TypeScript checks the types condition to locate declarations.
{
"name": "@acme/slugify",
"version": "1.0.0",
"type": "module",
"main": "./dist/index.cjs",
"module": "./dist/index.js",
"types": "./dist/index.d.ts",
"exports": {
".": {
"import": {
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
},
"require": {
"types": "./dist/index.d.cts",
"default": "./dist/index.cjs"
}
}
},
"files": ["dist"]
}The main, module, and top-level types fields help older tools. Modern Node.js and TypeScript should use exports.
Understand the build requirement
One TypeScript compilation normally targets one module format. A dual package therefore needs two builds, separate compiler configurations, or a build tool that can emit both formats.
The output must agree with the package metadata:
flowchart TD
source["TypeScript source"]
build["Two module-format builds"]
esm["dist/index.js + index.d.ts"]
cjs["dist/index.cjs + index.d.cts"]
exports["package.json exports"]
consumers["ESM and CommonJS consumers"]
source --> build
build --> esm
build --> cjs
esm --> exports
cjs --> exports
exports --> consumersCall the ESM build for import users and the CJS build for require users. Test both paths. A package can compile successfully while one branch still points to a missing file or exposes different names.
Choose dual packaging deliberately
Choose ESM-only when your consumers use modern Node.js and bundlers, and you want the simplest package. Choose dual packaging when compatibility with existing CommonJS applications is important.
Dual packaging costs more because it requires:
- Two runtime outputs.
- Two declaration paths.
- Tests for both import styles.
- Careful handling of shared state.
If ESM and CommonJS load separate copies of a stateful module, singleton behavior can differ. Keep public modules stateless where possible, or test identity-sensitive behavior explicitly.
Exercise
Which declaration file should the require condition use in a package with "type": "module"?
Show answer
It should use a CommonJS declaration such as index.d.cts, while the import condition uses an ESM declaration such as index.d.ts.