Skip to content
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

docs: document package.json's "exports" field #7323

Open
wants to merge 5 commits into
base: latest
Choose a base branch
from
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
63 changes: 63 additions & 0 deletions docs/lib/content/configuring-npm/package-json.md
Expand Up @@ -339,6 +339,69 @@ not much else.

If `main` is not set, it defaults to `index.js` in the package's root folder.

### exports

The exports field is an object that maps entry points to modules. This field is
supported by Node.js versions including and higher than 12. It acts as a more
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

higher than 12

see https://npmjs.com/node-exports-info - there’s actually a pretty complex set of feature support and node version matrix.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you think it would be better to take out the "higher than 12" comment since the version of NPM these docs are for only supports Node.js ^18.17.0 || >=20.5.0 anyway?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes - or maybe, "is supported by modern node.js versions"? also this should link to the node documentation of the field, which might be better than having examples here.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added your suggestions, how's this so far?

featureful alternative to the main field. Each key can be an explicit path for
mapping entry points to modules 1 to 1, or it can include a wild card (`*`) for
mapping multiple entry points that fit the wild card to corrosponding modules.

Each value in the exports field object can be an array of entry point string
where each path will be iterated through and the first path that leads to a
module will be used. The value also does not necessarily need to map to a
JavaScript module, it can also map to a JSON file.

For example, you could have:

```json
{
"exports": {
".": "./index.js",
"./*": "./*.js",
"./*.js": "./*.js",
"./foo": "./path/to/foo.js",
"./package.json": "./package.json"
}
}
```

If someone installed your package with this in your `package.json`, they could
`require("my-package")` and it would be mapped to
`./node_modules/my-package/index.js` because of `".": "./index.js"`.<br>
If they did `require("my-package/bar")` or `require("my-package/bar.js")`, it
would be mapped to `./node_modules/my-package/bar.js` because of the wild cards
(`*`).<br>
If they did `require("my-package/foo")` it would be mapped to
`./node_modules/my-package/path/to/foo.js` because of the explicit mapping
`"./foo": "./path/to/foo.js"`.

The exports field also supports conditional exports, which will use a different
object to resolve entry points to modules depending on if the consumer of your
package is using CommonJS `require()` calls or ESM `import`
statements/expressions.

Here is an example of conditional exports:

```json
"exports": {
"require": {
".": "./index.cjs",
"./*": "./*.cjs",
"./*.js": "./*.cjs",
"./foo": "./path/to/foo.cjs",
"./package.json": "./package.json"
},
"import": {
".": "./index.js",
"./*": "./*.js",
"./*.js": "./*.js",
"./foo": "./path/to/foo.js",
"./package.json": "./package.json"
}
}
```

### browser

If your module is meant to be used client-side the browser field should be
Expand Down