Sub topic 14
Loading ES Modules from CommonJS
This chapter assumes you know ES module syntax, async/await, and how Node.js decides whether a file is CommonJS or an ES module (.cjs, .mjs, and the type field in package.json). That last topic was covered earlier in this tutorial.
The Problem
Many Node.js codebases still use CommonJS, which loads code with require() and module.exports. More and more npm packages now ship only as ES modules. A CommonJS file cannot use a static import statement, and it cannot use top-level await. Older Node versions also refuse to require() an ES module and throw ERR_REQUIRE_ESM.
The import() expression works in both module systems. It is ordinary JavaScript syntax, so a CommonJS file can call it from inside any function.
The Basic Pattern
Put the work in an async function and await import() inside it.
// greet.mjs (ES module)
export function greet(name) {
return `Hello, ${name}!`;
}
export default function shout(name) {
return greet(name).toUpperCase();
}// main.cjs (CommonJS)
async function main() {
const { greet, default: shout } = await import('./greet.mjs');
console.log(greet('world'));
console.log(shout('world'));
}
main().catch((err) => {
console.error(err);
process.exitCode = 1;
});Run it with node main.cjs. The namespace object behaves exactly as it does in ES module code. Named exports are properties of the object, and the default export is the default property.
Calling It from Synchronous Code
Because import() is asynchronous, a CommonJS module cannot hand an ES module's exports straight back through module.exports. A common workaround is to cache the promise and export an async accessor:
// lib.cjs
let greetModule;
function loadGreet() {
greetModule ??= import('./greet.mjs');
return greetModule;
}
module.exports = {
async greet(name) {
const mod = await loadGreet();
return mod.greet(name);
},
};Each caller gets the same module instance, because Node caches the module once it has been loaded.
What About require(esm)?
Node.js 22.12 and 20.19 let you require() an ES module without a flag. The rule is that the module's whole graph must evaluate synchronously. If any module in that graph uses top-level await, require() throws ERR_REQUIRE_ASYNC_MODULE. import() has no such limit, and it also works on older Node versions.
| Approach | Works with top-level await | Sync result | Node support |
|---|---|---|---|
| `require('./x.mjs')` | No | Yes | 20.19+, 22.12+ |
| `await import('./x.mjs')` | Yes | No (promise) | 12+ |
A Common Error: Transpilers Rewriting import()
TypeScript with "module": "commonjs", and some Babel setups, rewrite import() into require(). When that happens, the ERR_REQUIRE_ESM error comes back even though your source looks correct.
Exercise
A CommonJS script runs const { default: chalk } = require('chalk'); and gets ERRREQUIREESM on Node 18. Rewrite the script so it prints "ok" in green using chalk v5, which ships only as an ES module.
Show answer
Wrap the code in an async function and use import(): async function main() { const { default: chalk } = await import('chalk'); console.log(chalk.green('ok')); } main(); The default export is read from the namespace object's default property.
Exercise
Your CommonJS code calls require() on an ES module on Node 22.12 and gets ERRREQUIREASYNC_MODULE. What causes this, and what is the fix?
Show answer
Some module in the required graph uses top-level await, so it cannot be evaluated synchronously. Load it with await import() inside an async function.