Sub topic 9
Importing CommonJS from ESM and the Reverse
Assume you can write TypeScript imports and exports, understand tsconfig.json, and know that Node.js supports both CommonJS and ES modules. This chapter focuses on the boundary between those formats under module: "nodenext".
The two module formats
CommonJS uses require and module.exports:
// legacy.cts
function greet(name: string) {
return `Hello, ${name}`;
}
module.exports = { greet };An ES module uses import and export:
// main.mts
import legacy from "./legacy.cjs";
console.log(legacy.greet("Ada"));The .cts file compiles to .cjs, and the .mts file compiles to .mjs. The explicit extensions make the runtime format clear to Node.js.
When an ES module imports CommonJS, Node.js exposes the CommonJS value as the ES module's default export. This is the most reliable form:
import legacy from "./legacy.cjs";
legacy.greet("Ada");Node.js may also detect named exports from CommonJS source:
import { greet } from "./legacy.cjs";
console.log(greet("Ada"));Named imports are less reliable because Node.js analyzes CommonJS statically. It does not execute the module first and inspect every property added to module.exports. Prefer the default import when you do not control the CommonJS package.
Importing ESM from CommonJS
A CommonJS module cannot generally use a top-level static import statement. Use dynamic import() instead:
// loader.cts
async function loadGreeting() {
const mod = await import("./esm-only.mjs");
return mod.default("Ada");
}
loadGreeting().then(console.log);The returned value is a promise for a module namespace object. The namespace contains the module's named exports and its default export.
The ES module might look like this:
// esm-only.mts
export default function greet(name: string) {
return `Hello, ${name}`;
}A dynamic import is asynchronous, so callers must use await or a promise callback. This is the portable solution when CommonJS code needs an ES module.
A static require() call is different:
const mod = require("./esm-only.mjs");This can fail at runtime because loading an ES module may require asynchronous evaluation. Some newer Node.js versions support synchronous require() for certain ES modules, but code that must work across Node.js versions should use import().
How TypeScript checks the boundary
With module: "nodenext", TypeScript models the format of the output and the rules used by Node.js. It can report an error when a CommonJS file statically imports an ES module, because that import would become an incompatible require() call.
The esModuleInterop option does not convert a CommonJS file into an ES module. It mainly changes how TypeScript emits helper code for certain CommonJS-style imports. It cannot make Node.js accept an invalid module-format transition.
Think of the boundary this way:
A practical checklist
- Use a default import when ESM consumes a CommonJS module.
- Use
import()when CommonJS consumes an ES module. - Include
.cjsor.mjsin compiled relative imports where required. - Do not assume CommonJS named exports are always detectable.
- Test the emitted JavaScript with the Node.js version used in production.
- Treat TypeScript's interop settings and Node.js runtime behavior as separate concerns.
Exercise
A .cts file needs to load a function exported as the default from an .mts file. Which mechanism should it use, and where is the function found?
Show answer
It should use const mod = await import("./file.mjs"). The function is found at mod.default.