You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/lib/content/configuring-npm/package-json.md
+47-5Lines changed: 47 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -341,7 +341,16 @@ If `main` is not set, it defaults to `index.js` in the package's root folder.
341
341
342
342
### exports
343
343
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.
345
354
346
355
For example, you could have:
347
356
@@ -351,14 +360,47 @@ For example, you could have:
351
360
".": "./index.js",
352
361
"./*": "./*.js",
353
362
"./*.js": "./*.js",
354
-
"./foo": "./path/to/foo.js"
363
+
"./foo": "./path/to/foo.js",
364
+
"./package.json": "./package.json"
355
365
}
356
366
}
357
367
```
358
368
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`
0 commit comments