-
Notifications
You must be signed in to change notification settings - Fork 173
doc: add require(esm) explainer #147
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
vinayakPandey7
wants to merge
1
commit into
nodejs:main
Choose a base branch
from
vinayakPandey7:docs/require-esm
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| --- | ||
| authors: vinayakPandey7 | ||
| --- | ||
|
|
||
| # Requiring ES modules from CommonJS | ||
|
|
||
| Node.js can load an ES module with `require()`. That is often called **require(esm)**. | ||
|
|
||
| It lets CommonJS code (`require` / `module.exports`) load packages that only ship ESM (`export` / `import`), without converting the whole app to ESM first. | ||
|
|
||
| The feature is unflagged on current release lines, so you do not need `--experimental-require-module`. It was marked no longer experimental in Node.js 25.4.0. | ||
|
|
||
| ## Versions | ||
|
|
||
| Use a Node.js version that has unflagged support: | ||
|
|
||
| - **Node.js 20.19.0** or later on the v20 line | ||
| - **Node.js 22.12.0** or later | ||
| - Any later major (23+) | ||
|
|
||
| A common `engines` range is: | ||
|
|
||
| ```json | ||
| { | ||
| "engines": { | ||
| "node": "^20.19.0 || >=22.12.0" | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| On older versions, `require()` of an ES module throws `ERR_REQUIRE_ESM`. | ||
|
|
||
| ## Quick example | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you use our tabbed code box for conformity ( |
||
|
|
||
| `distance.mjs`: | ||
|
|
||
| ```mjs | ||
| export function distance(a, b) { | ||
| return Math.sqrt((b.x - a.x) ** 2 + (b.y - a.y) ** 2); | ||
| } | ||
| ``` | ||
|
|
||
| `app.js` (CommonJS): | ||
|
|
||
| ```cjs | ||
| const { distance } = require('./distance.mjs'); | ||
|
|
||
| console.log(distance({ x: 0, y: 0 }, { x: 3, y: 4 })); | ||
| // 5 | ||
| ``` | ||
|
|
||
| `require()` of an ES module returns the **module namespace object**, the same shape as a static `import`, including a `.default` property when the module has a default export. | ||
|
|
||
| `point.mjs`: | ||
|
|
||
| ```mjs | ||
| export default class Point { | ||
| constructor(x, y) { | ||
| this.x = x; | ||
| this.y = y; | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| ```cjs | ||
| const point = require('./point.mjs'); | ||
|
|
||
| console.log(point); | ||
| // [Module: null prototype] { default: [class Point], __esModule: true } | ||
|
|
||
| const Point = point.default; | ||
| ``` | ||
|
|
||
| The ES module may also export the name `"module.exports"` so `require()` returns that value directly. See the [API docs](https://nodejs.org/api/modules.html#loading-ecmascript-modules-using-require). | ||
|
|
||
| ## When `require()` can load ESM | ||
|
|
||
| The module must be **fully synchronous** (no top-level `await`), and one of these must be true: | ||
|
|
||
| 1. The file ends in `.mjs` | ||
| 2. The file ends in `.js` and the closest `package.json` has `"type": "module"` | ||
| 3. The file ends in `.js`, the closest `package.json` is not `"type": "commonjs"`, and the source uses ES module syntax | ||
|
|
||
| Graph modules loaded from that ESM file can use `import` as usual. | ||
|
|
||
| ## FAQ | ||
|
|
||
| ### Can the ES module use top-level `await`? | ||
|
|
||
| No. Top-level `await` makes the module async. `require()` is synchronous, so that load fails. Use dynamic `import()` if you need TLA. | ||
|
|
||
| ### Is `require(esm)` the same as `import()`? | ||
|
|
||
| No. `import()` is async and returns a promise. `require(esm)` runs synchronously and returns the namespace (or `module.exports` interop) immediately. Prefer `import` in new ESM code; use `require(esm)` when you are still in CommonJS. | ||
|
|
||
| ### Do I still need dual CJS+ESM builds? | ||
|
|
||
| Often no, if your consumers are on the versions above. Publishing a single ESM graph that is `require()`-able is simpler and avoids [dual-package hazard](/learn/modules/publishing-a-package). Check your support policy first. | ||
|
|
||
| ### How do I tell if a package is ESM? | ||
|
|
||
| Look at `"type": "module"` and `"exports"` in its `package.json`. If `require()` used to throw `ERR_REQUIRE_ESM` and you are now on a supported Node.js, try again without extra flags. | ||
|
|
||
| ## Learn more | ||
|
|
||
| - [Loading ECMAScript modules using `require()`](https://nodejs.org/api/modules.html#loading-ecmascript-modules-using-require) — API details | ||
| - [Determining the module system](https://nodejs.org/api/packages.html#determining-module-system) | ||
| - [Publishing a package](/learn/modules/publishing-a-package) | ||
| - [package-examples: making ESM require-able](https://github.com/nodejs/package-examples/blob/main/guide/04-cjs-esm-interop/require-esm/README.md) — worked examples | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -333,6 +333,10 @@ | |
| { | ||
| "groupName": "Modules", | ||
| "items": [ | ||
| { | ||
| "link": "/learn/modules/require-esm", | ||
| "label": "Requiring ES modules from CommonJS" | ||
| }, | ||
|
Comment on lines
+336
to
+339
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think this should be first, IMO, it's a bit advanced... |
||
| { | ||
| "link": "/learn/modules/how-to-use-streams", | ||
| "label": "How to use streams" | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All supported lines allow this, so you don't need to specify