Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 56b150b

Browse filesBrowse files
GeoffreyBoothBridgeAR
authored andcommitted
doc: add current recommendation for ESM/CommonJS dual packages
PR-URL: #27957 Reviewed-By: Ujjwal Sharma <usharma1998@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Jan Krems <jan.krems@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Bradley Farias <bradley.meck@gmail.com>
1 parent 360c708 commit 56b150b
Copy full SHA for 56b150b

File tree

Expand file treeCollapse file tree

1 file changed

+9
-10
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

1 file changed

+9
-10
lines changed
Open diff view settings
Collapse file

‎doc/api/esm.md‎

Copy file name to clipboardExpand all lines: doc/api/esm.md
+9-10Lines changed: 9 additions & 10 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -206,16 +206,15 @@ a full path including extension: `"./index.mjs"`, not `"./index"`.
206206
If the `package.json` `"type"` field is omitted, a `.js` file in `"main"` will
207207
be interpreted as CommonJS.
208208

209-
> Currently a package can define _either_ a CommonJS entry point **or** an ES
210-
> module entry point; there is no way to specify separate entry points for
211-
> CommonJS and ES module usage. This means that a package entry point can be
212-
> included via `require` or via `import` but not both.
213-
>
214-
> Such a limitation makes it difficult for packages to support both new versions
215-
> of Node.js that understand ES modules and older versions of Node.js that
216-
> understand only CommonJS. There is work ongoing to remove this limitation, and
217-
> it will very likely entail changes to the behavior of `"main"` as defined
218-
> here.
209+
The `"main"` field can point to exactly one file, regardless of whether the
210+
package is referenced via `require` (in a CommonJS context) or `import` (in an
211+
ES module context). Package authors who want to publish a package to be used in
212+
both contexts can do so by setting `"main"` to point to the CommonJS entry point
213+
and informing the package’s users of the path to the ES module entry point. Such
214+
a package would be accessible like `require('pkg')` and `import
215+
'pkg/module.mjs'`. Alternatively the package `"main"` could point to the ES
216+
module entry point and legacy users could be informed of the CommonJS entry
217+
point path, e.g. `require('pkg/commonjs')`.
219218

220219
## <code>import</code> Specifiers
221220

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.