Skip to content

Commit 7a38e9f

Browse files
committed
docs: document package.json's "exports" fields' conditional exports
1 parent e17d3c1 commit 7a38e9f

1 file changed

Lines changed: 47 additions & 5 deletions

File tree

docs/lib/content/configuring-npm/package-json.md

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,16 @@ If `main` is not set, it defaults to `index.js` in the package's root folder.
341341

342342
### exports
343343

344-
The exports field is an object that maps entry points to modules. It supports wild cards (`*`) and explicit names.
344+
The exports field is an object that maps entry points to modules. This field is
345+
supported by Node.js versions including and higher than 12. It acts as a more
346+
featureful alternative to the main field. Each key can be an explicit path for
347+
mapping entry points to modules 1 to 1, or it can include a wild card (`*`) for
348+
mapping multiple entry points that fit the wild card to corrosponding modules.
349+
350+
Each value in the exports field object can be an array of entry point string
351+
where each path will be iterated through and the first path that leads to a
352+
module will be used. The value also does not necessarily need to map to a
353+
JavaScript module, it can also map to a JSON file.
345354

346355
For example, you could have:
347356

@@ -351,14 +360,47 @@ For example, you could have:
351360
".": "./index.js",
352361
"./*": "./*.js",
353362
"./*.js": "./*.js",
354-
"./foo": "./path/to/foo.js"
363+
"./foo": "./path/to/foo.js",
364+
"./package.json": "./package.json"
355365
}
356366
}
357367
```
358368

359-
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>
360-
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>
361-
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"`.<br>
369+
If someone installed your package with this in your `package.json`, they could
370+
`require("my-package")` and it would be mapped to
371+
`./node_modules/my-package/index.js` because of `".": "./index.js"`.<br>
372+
If they did `require("my-package/bar")` or `require("my-package/bar.js")`, it
373+
would be mapped to `./node_modules/my-package/bar.js` because of the wild cards
374+
(`*`).<br>
375+
If they did `require("my-package/foo")` it would be mapped to
376+
`./node_modules/my-package/path/to/foo.js` because of the explicit mapping
377+
`"./foo": "./path/to/foo.js"`.
378+
379+
The exports field also supports conditional exports, which will use a different
380+
object to resolve entry points to modules depending on if the consumer of your
381+
package is using CommonJS `require()` calls or ESM `import`
382+
statements/expressions.
383+
384+
Here is an example of conditional exports:
385+
386+
```json
387+
"exports": {
388+
"require": {
389+
".": "./index.cjs",
390+
"./*": "./*.cjs",
391+
"./*.js": "./*.cjs",
392+
"./foo": "./path/to/foo.cjs",
393+
"./package.json": "./package.json"
394+
},
395+
"import": {
396+
".": "./index.js",
397+
"./*": "./*.js",
398+
"./*.js": "./*.js",
399+
"./foo": "./path/to/foo.js",
400+
"./package.json": "./package.json"
401+
}
402+
}
403+
```
362404

363405
### browser
364406

0 commit comments

Comments
 (0)