Sub topic 2
ESM and CommonJS: how a file's format is decided
This chapter assumes that you can write TypeScript import and export statements, edit tsconfig.json, and run tsc. It also assumes that you know Node.js supports both ES modules (ESM) and CommonJS (CJS).
The same TypeScript syntax can produce different module behavior. TypeScript must first decide whether a source file is ESM or CJS. That decision affects import rules, emitted JavaScript, and how Node.js loads the result.
ESM and CommonJS are different formats
A module format defines how a file exports values and imports other files.
ESM is the standard JavaScript module format. It uses import and export statements. Browsers and modern Node.js support it directly.
CommonJS is Node.js's older module format. It commonly uses require() and module.exports.
| Format | Import style | Export style | Typical Node.js extensions |
|---|---|---|---|
| ESM | `import x from "x"` | `export` | `.mjs` |
| CommonJS | `const x = require("x")` | `module.exports` | `.cjs` |
TypeScript adds extensions that make the intended output clear:
.mtsis always treated as ESM and normally emits.mjs..ctsis always treated as CommonJS and normally emits.cjs..tsis treated according to the nearestpackage.json..mjsand.cjsdescribe JavaScript files that TypeScript reads as ESM and CommonJS.
How nodenext decides
With "module": "nodenext", TypeScript follows Node.js's rules. It examines the file extension and the nearest package boundary.
A package boundary is a directory containing package.json. The "type" field in that file controls ordinary .js files and, under nodenext, ordinary .ts files in the same package scope.
{
"name": "weather-app",
"type": "module",
"scripts": {
"build": "tsc"
}
}In this package:
src/weather.tsis treated as ESM.src/legacy.ctsis treated as CommonJS.src/explicit.mtsis treated as ESM.- A nested package with
"type": "commonjs"can change the meaning of its own.tsfiles.
If "type" is "commonjs" or is missing, ordinary .ts files in that package are treated as CommonJS. The explicit .mts and .cts extensions still override the package setting.
The compiler uses this information twice. It chooses the module format for the emitted JavaScript, and it checks whether the source imports are valid for that format.
The same syntax can have different results
Consider this source file:
// src/main.ts
import { readFile } from "node:fs/promises";
export async function readText(path: string): Promise<string> {
return readFile(path, "utf8");
}When the nearest package has "type": "module", TypeScript treats this file as ESM. When the package has "type": "commonjs", TypeScript treats it as CommonJS under nodenext. The source still contains import and export, but the emitted module format differs.
An ESM file must follow ESM runtime rules. For example, Node.js does not automatically add a missing extension to a relative ESM import. A CommonJS file follows different loading rules and can use require() in the emitted output.
Visualizing the decision
Common mistakes
A frequent mistake is assuming that every .ts file is ESM because it uses import syntax. Under nodenext, the package scope decides unless the filename uses .mts or .cts.
Another mistake is changing "type" without checking all package files. That change can alter the runtime interpretation of emitted .js files and can expose missing extensions in relative imports.
Use .mts and .cts when a file must keep a specific format regardless of its package context. Use ordinary .ts when the file should follow the package's normal convention.
Exercise
A project uses "module": "nodenext" and has "type": "module" in its nearest package.json. What formats do src/app.ts, src/test.cts, and src/config.mts use?
Show answer
src/app.ts is ESM because the package is typed as a module. src/test.cts is CommonJS because .cts explicitly selects CommonJS. src/config.mts is ESM because .mts explicitly selects ESM.