Sub topic 4
CommonJS versus ES modules in Node.js
Assume you know basic TypeScript types, import and export syntax, tsc, tsconfig.json, and package.json. This chapter focuses on how Node.js loads modules and how TypeScript maps its module syntax to Node.js behavior.
A module format is the set of rules a runtime uses to load and connect files. Node.js supports two main formats:
- CommonJS (CJS) uses
require()to import values andmodule.exportsto export them. - ECMAScript modules (ESM) use
importandexport.
TypeScript can check both formats, but Node.js still decides how the emitted JavaScript runs.
CommonJS
CommonJS is the older Node.js module system. A file is treated as CommonJS when its package configuration or file extension selects that format.
// math.cts
export function add(a: number, b: number): number {
return a + b;
}With a Node-aware TypeScript configuration, a .cts file emits a .cjs file. TypeScript converts the export into CommonJS-compatible JavaScript. A CommonJS consumer can load it with require():
const { add } = require("./math.cjs");
console.log(add(2, 3));CommonJS modules load synchronously. The require() call returns the value exported by the target module.
ES modules
ESM is the JavaScript standard module system. In a package whose package.json contains "type": "module", ordinary .js files are ESM. TypeScript source files use the same package rules when you compile with module: "nodenext".
{
"type": "module"
}// math.ts
export function add(a: number, b: number): number {
return a + b;
}
// main.ts
import { add } from "./math.js";
console.log(add(2, 3));The .js extension in the TypeScript import is intentional. Node.js executes the emitted JavaScript, so the runtime path must name the emitted .js file. TypeScript understands that ./math.js refers to the source file math.ts during type checking.
ESM imports are static. The runtime knows the module's dependencies before evaluating the module body.
Choosing the format in TypeScript
For modern Node.js projects, nodenext is usually the best choice because it follows Node.js rules for both module format and module resolution.
{
"compilerOptions": {
"target": "es2022",
"module": "nodenext",
"strict": true,
"outDir": "dist"
},
"include": ["src"]
}With nodenext:
.mtsfiles emit ESM.mjsfiles..ctsfiles emit CommonJS.cjsfiles..tsfiles follow the nearestpackage.json"type"field.- Relative ESM imports normally need their runtime
.jsextension.
This lets one project contain both formats when a compatibility boundary requires it.
Interoperability and common errors
ESM and CommonJS can interoperate, but their export models differ. A CommonJS module exports one module.exports value. ESM has named exports and a default export. Node.js can expose some CommonJS properties as named imports, but this detection is limited and can fail for dynamically assigned properties.
A common error is using require() inside an ESM file. Use a static import instead. Conversely, a CommonJS file cannot always use top-level import syntax without changing its format.
Another common error is omitting .js from an ESM relative import. TypeScript may understand the source path, but Node.js resolves the emitted path and can report ERR_MODULE_NOT_FOUND.
> callout note > Choose one module format for an application unless you have a clear compatibility reason to mix them. Mixing formats increases the number of valid-looking import forms that can fail at runtime.
Exercise
In a package with "type": "module" and "module": "nodenext", which source file extension should you use when you need a CommonJS file, and what extension will TypeScript emit?
Show answer
Use .cts. TypeScript emits a .cjs file, which Node.js treats as CommonJS.