Sub topic 4
ESM and CommonJS: What the File Extension and `type` Field Mean
This chapter assumes you can read TypeScript imports and exports, use npm, and run tsc. It also assumes you know that a package can contain both source files and compiled files. The key question here is how Node.js decides whether a JavaScript file uses ESM or CommonJS.
Two module formats
ESM, or ECMAScript Modules, is the JavaScript standard module system. It uses import and export.
CommonJS is Node.js's older module system. It uses require and module.exports.
| Format | Import syntax | Export syntax | Common file extensions |
|---|---|---|---|
| ESM | `import x from "x"` | `export` | `.mjs`, `.js` in a module package |
| CommonJS | `require("x")` | `module.exports` | `.cjs`, `.js` in a CommonJS package |
A package can choose its default format with the type field in package.json:
{
"name": "@acme/slugify",
"version": "1.0.0",
"type": "module"
}With "type": "module", Node treats .js files in that package as ESM. Without this field, or with "type": "commonjs", Node treats .js files as CommonJS.
File extensions override the default
The extensions .mjs and .cjs explicitly select a format:
.mjsis always ESM..cjsis always CommonJS..jsfollows the nearestpackage.jsontypefield.
This rule is useful when one package needs both formats. It also prevents an ambiguous file from changing behavior when moved between packages.
TypeScript adds two related source extensions:
.mtsalways compiles as ESM..ctsalways compiles as CommonJS..tsfollows the package context when usingmoduleset toNodeNextorNode16.
For example, these files use different systems even in the same package:
// src/esm.mts
export function greet(name: string): string {
return `Hello, ${name}`;
}
// src/commonjs.cts
function farewell(name: string): string {
return `Goodbye, ${name}`;
}
export = farewell;A typical compiler output is esm.mjs and commonjs.cjs. Their extensions tell Node how to load them.
What this means for a library
An ESM library normally has source such as:
// src/index.ts
export function slugify(input: string): string {
return input.trim().toLowerCase().replace(/\s+/g, "-");
}When the package is ESM, relative imports in emitted JavaScript usually need their final .js extension:
export { slugify } from "./slugify.js";The extension refers to the emitted JavaScript file, even though the source file is slugify.ts.
A CommonJS consumer usually writes:
const { slugify } = require("@acme/slugify");An ESM consumer writes:
import { slugify } from "@acme/slugify";These consumers cannot always interchange formats. Calling require() on an ESM-only package commonly fails because CommonJS cannot synchronously load an ESM module.
Choosing a format
Choose ESM-only when your users run modern Node.js, browsers, or bundlers and you want one simpler output format. ESM supports the current JavaScript standard and avoids maintaining two builds.
Choose dual ESM and CommonJS output when existing Node.js applications still use require(). Dual packages require separate outputs, compatible declarations, and careful export conditions. A mistake can make the JavaScript format and the TypeScript types disagree.
When TypeScript builds a library, use module: "NodeNext" or module: "Node16" when you want TypeScript's resolution rules to match Node's rules. Using an older setting while publishing Node-style ESM can hide import and extension errors until consumers install the package.
Exercise
In a package with "type": "module", what formats do util.js, legacy.cjs, and modern.mjs use?
Show answer
util.js and modern.mjs use ESM. legacy.cjs uses CommonJS because .cjs overrides the package default.