Skip to content

Commit

Permalink
doc: improve explanation of package.json "type" field
Browse files Browse the repository at this point in the history
Regardless of the value of the `"type"` field, `.mjs` files are
always treated as ES modules and `.cjs` files are always treated
as CommonJS.

PR-URL: #27516
Reviewed-By: Bradley Farias <bradley.meck@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
  • Loading branch information
tamias authored and BethGriggs committed Feb 6, 2020
1 parent 5edd1a2 commit deb1a59
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions doc/api/esm.md
Expand Up @@ -62,9 +62,9 @@ or when referenced by `import` statements within ES module code:

### `package.json` `"type"` field

Files ending with `.js` or `.mjs`, or lacking any extension,
will be loaded as ES modules when the nearest parent `package.json` file
contains a top-level field `"type"` with a value of `"module"`.
Files ending with `.js` or lacking any extension will be loaded as ES modules
when the nearest parent `package.json` file contains a top-level field `"type"`
with a value of `"module"`.

The nearest parent `package.json` is defined as the first `package.json` found
when searching in the current folder, that folder’s parent, and so on up
Expand Down Expand Up @@ -104,6 +104,9 @@ future-proof the package in case the default type of Node.js ever changes, and
it will also make things easier for build tools and loaders to determine how the
files in the package should be interpreted.

Regardless of the value of the `"type"` field, `.mjs` files are always treated
as ES modules and `.cjs` files are always treated as CommonJS.

### Package Scope and File Extensions

A folder containing a `package.json` file, and all subfolders below that
Expand Down

0 comments on commit deb1a59

Please sign in to comment.