Node.js Reference ยท Module Systems
require(esm): loading ES modules from CommonJS
Reference for require(esm) in Node.js: how CommonJS code can load synchronous ES modules, what the result looks like, the module.exports export name and its limits.
For years a CommonJS file could not require() an ES module; it had to use the asynchronous import() instead. Current Node.js releases (22.12 and newer, including Node.js 24 LTS) lift that limit: require() can load an ES module synchronously, as long as neither the module nor anything it imports uses top-level await.
This matters most for package authors. A library can now ship only ES modules, and its CommonJS users still get it with a plain require('lib'), without a second build. The result of require() is the module namespace object, so named exports are properties and the default export is under .default.
// cjs const ns = require('./math.mjs'); console.log(ns.add(1, 2), ns.default);
Try it
The first line // cjs of these editors makes the runner treat the code as CommonJS, so require is available.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.
How it works
When require() meets a file that is an ES module (by extension, by the "type" field, or by syntax detection), Node.js loads it with the ES module loader, runs it synchronously and returns its namespace object. The usual module cache is shared, so a module that is loaded with import and with require is the same instance in both.
| Situation | Result |
|---|---|
| Module with only named exports | Namespace object with those properties |
| Module with a default export | The value is at ns.default; ns.__esModule is true so that transpiled code interops |
Module that exports a name 'module.exports' |
require() returns that value directly instead of the namespace |
Module or dependency with top-level await |
Throws ERR_REQUIRE_ASYNC_MODULE |
| Circular require with the ES module in the cycle | May throw ERR_REQUIRE_CYCLE_MODULE |
Usage notes
- Version. Enabled without a flag in Node.js 22.12 and 20.19. In older releases
require()of an ES module throwsERR_REQUIRE_ESM. - Experimental warning. Some versions print an
ExperimentalWarningthe first time; it is no longer shown in current LTS releases for code innode_modules. Use--no-experimental-require-moduleto turn the feature off. - The
__esModuleflag is added so that code compiled by tools which expect it readsns.defaultcorrectly, like with Babel or TypeScript output. - Prefer a named
'module.exports'when a package used to export a single function from CommonJS and wants to keepconst fn = require('pkg')working after moving to ES modules. - Keep ES modules synchronous if people may require them. Move network or file waits into exported async functions instead of top-level
await. - Not a live view. The namespace object itself is live, but destructuring
const { count } = require(...)copies the value at that moment. - Dual packages are less needed now. A package with
"exports": { "require": ..., "import": ... }can point both at the same ES file; see Conditional exports. - Import in the other direction needs no support: an ES module can always
importa CommonJS file.