Skip to content

Commit 539c33d

Browse files
committed
doc: document support for package.json fields
Fixes: nodejs#33143
1 parent dbc5c17 commit 539c33d

File tree

4 files changed

+264
-133
lines changed

4 files changed

+264
-133
lines changed

doc/api/errors.md

+12-10
Original file line numberDiff line numberDiff line change
@@ -1391,13 +1391,13 @@ An invalid or unknown file encoding was passed.
13911391
<a id="ERR_INVALID_PACKAGE_CONFIG"></a>
13921392
### `ERR_INVALID_PACKAGE_CONFIG`
13931393

1394-
An invalid `package.json` file was found which failed parsing.
1394+
An invalid [`package.json`][] file was found which failed parsing.
13951395

13961396
<a id="ERR_INVALID_PACKAGE_TARGET"></a>
13971397
### `ERR_INVALID_PACKAGE_TARGET`
13981398

1399-
The `package.json` [exports][] field contains an invalid target mapping value
1400-
for the attempted module resolution.
1399+
The [`package.json`][] [`"exports"`][] field contains an invalid target mapping
1400+
value for the attempted module resolution.
14011401

14021402
<a id="ERR_INVALID_PERFORMANCE_MARK"></a>
14031403
### `ERR_INVALID_PERFORMANCE_MARK`
@@ -1730,15 +1730,16 @@ A given value is out of the accepted range.
17301730
<a id="ERR_PACKAGE_IMPORT_NOT_DEFINED"></a>
17311731
### `ERR_PACKAGE_IMPORT_NOT_DEFINED`
17321732

1733-
The `package.json` ["imports" field][] does not define the given internal
1733+
The [`package.json`][] [`"imports"`][] field does not define the given internal
17341734
package specifier mapping.
17351735

17361736
<a id="ERR_PACKAGE_PATH_NOT_EXPORTED"></a>
17371737
### `ERR_PACKAGE_PATH_NOT_EXPORTED`
17381738

1739-
The `package.json` [exports][] field does not export the requested subpath.
1740-
Because exports are encapsulated, private internal modules that are not exported
1741-
cannot be imported through the package resolution, unless using an absolute URL.
1739+
The [`package.json`][] [`"exports"`][] field does not export the requested
1740+
subpath. Because exports are encapsulated, private internal modules that are not
1741+
exported cannot be imported through the package resolution, unless using an
1742+
absolute URL.
17421743

17431744
<a id="ERR_PROTO_ACCESS"></a>
17441745
### `ERR_PROTO_ACCESS`
@@ -2125,7 +2126,7 @@ signal (such as [`subprocess.kill()`][]).
21252126

21262127
`import` a directory URL is unsupported. Instead,
21272128
[self-reference a package using its name][] and [define a custom subpath][] in
2128-
the `"exports"` field of the `package.json` file.
2129+
the [`"exports"`][] field of the [`package.json`][] file.
21292130

21302131
<!-- eslint-skip -->
21312132
```js
@@ -2645,7 +2646,8 @@ closed.
26452646
[crypto digest algorithm]: crypto.html#crypto_crypto_gethashes
26462647
[domains]: domain.html
26472648
[event emitter-based]: events.html#events_class_eventemitter
2648-
[exports]: packages.html#packages_package_entry_points
2649+
[`package.json`]: packages.html#packages_package_json_supported_fields
2650+
[`"exports"`]: packages.html#packages_exports
26492651
[file descriptors]: https://en.wikipedia.org/wiki/File_descriptor
26502652
[policy]: policy.html
26512653
[RFC 7230 Section 3]: https://tools.ietf.org/html/rfc7230#section-3
@@ -2656,4 +2658,4 @@ closed.
26562658
[vm]: vm.html
26572659
[self-reference a package using its name]: packages.html#packages_self_referencing_a_package_using_its_name
26582660
[define a custom subpath]: packages.html#packages_subpath_exports
2659-
["imports" field]: packages.html#packages_internal_package_imports
2661+
[`"imports"`]: packages.html#packages_imports

doc/api/esm.md

+13-10
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ Expect major changes in the implementation including interoperability support,
4545
specifier resolution, and default behavior.
4646

4747
<!-- Anchors to make sure old links find a target -->
48-
<i id="#esm_package_json_type_field"></i>
48+
<i id="#esm_packages_type_field"></i>
4949
<i id="#esm_package_scope_and_file_extensions"></i>
5050
<i id="#esm_input_type_flag"></i>
5151

@@ -55,7 +55,7 @@ specifier resolution, and default behavior.
5555

5656
Node.js treats JavaScript code as CommonJS modules by default.
5757
Authors can tell Node.js to treat JavaScript code as ECMAScript modules
58-
via the `.mjs` file extension, the `package.json` `"type"` field, or the
58+
via the `.mjs` file extension, the `package.json` [`"type"`][] field, or the
5959
`--input-type` flag. See
6060
[Modules: Packages](packages.html#packages_determining_module_system) for more
6161
details.
@@ -253,9 +253,9 @@ can either be an URL-style relative path like `'./file.mjs'` or a package name
253253
like `'fs'`.
254254
255255
Like in CommonJS, files within packages can be accessed by appending a path to
256-
the package name; unless the package’s `package.json` contains an `"exports"`
257-
field, in which case files within packages need to be accessed via the path
258-
defined in `"exports"`.
256+
the package name; unless the package’s [`package.json`][] contains an
257+
[`"exports"`][] field, in which case files within packages need to be accessed
258+
via the path defined in [`"exports"`][].
259259
260260
```js
261261
import { sin, cos } from 'geometry/trigonometry-functions.mjs';
@@ -933,7 +933,7 @@ The resolver can throw the following errors:
933933
> 1. If the folder at _packageURL_ does not exist, then
934934
> 1. Set _parentURL_ to the parent URL path of _parentURL_.
935935
> 1. Continue the next loop iteration.
936-
> 1. Let _pjson_ be the result of **READ_PACKAGE_JSON**(_packageURL_).
936+
> 1. Let _pjson_ be the result of **READ_packages**(_packageURL_).
937937
> 1. If _pjson_ is not **null** and _pjson_._exports_ is not **null** or
938938
> **undefined**, then
939939
> 1. Let _exports_ be _pjson.exports_.
@@ -953,7 +953,7 @@ The resolver can throw the following errors:
953953
> 1. Let _packageURL_ be the result of **READ_PACKAGE_SCOPE**(_parentURL_).
954954
> 1. If _packageURL_ is **null**, then
955955
> 1. Return **undefined**.
956-
> 1. Let _pjson_ be the result of **READ_PACKAGE_JSON**(_packageURL_).
956+
> 1. Let _pjson_ be the result of **READ_packages**(_packageURL_).
957957
> 1. If _pjson_ is **null** or if _pjson_._exports_ is **null** or
958958
> **undefined**, then
959959
> 1. Return **undefined**.
@@ -995,7 +995,7 @@ The resolver can throw the following errors:
995995
> 1. Throw an _Invalid Module Specifier_ error.
996996
> 1. Let _packageURL_ be the result of **READ_PACKAGE_SCOPE**(_parentURL_).
997997
> 1. If _packageURL_ is not **null**, then
998-
> 1. Let _pjson_ be the result of **READ_PACKAGE_JSON**(_packageURL_).
998+
> 1. Let _pjson_ be the result of **READ_packages**(_packageURL_).
999999
> 1. If _pjson.imports_ is a non-null Object, then
10001000
> 1. Let _resolvedMatch_ be the result of
10011001
> **PACKAGE_IMPORTS_EXPORTS_RESOLVE**(_specifier_, _pjson.imports_,
@@ -1091,12 +1091,12 @@ _conditions_)
10911091
> 1. While _scopeURL_ is not the file system root,
10921092
> 1. Set _scopeURL_ to the parent URL of _scopeURL_.
10931093
> 1. If _scopeURL_ ends in a _"node_modules"_ path segment, return **null**.
1094-
> 1. Let _pjson_ be the result of **READ_PACKAGE_JSON**(_scopeURL_).
1094+
> 1. Let _pjson_ be the result of **READ_packages**(_scopeURL_).
10951095
> 1. If _pjson_ is not **null**, then
10961096
> 1. Return _pjson_.
10971097
> 1. Return **null**.
10981098

1099-
**READ_PACKAGE_JSON**(_packageURL_)
1099+
**READ_packages**(_packageURL_)
11001100

11011101
> 1. Let _pjsonURL_ be the resolution of _"package.json"_ within _packageURL_.
11021102
> 1. If the file at _pjsonURL_ does not exist, then
@@ -1159,3 +1159,6 @@ success!
11591159
[6.1.7 Array Index]: https://tc39.es/ecma262/#integer-index
11601160
[Top-Level Await]: https://github.com/tc39/proposal-top-level-await
11611161
[Core modules]: modules.html#modules_core_modules
1162+
[`package.json`]: packages.html#packages_package_json_supported_fields
1163+
[`"exports"`]: packages.html#packages_exports
1164+
[`"type"`]: packages.html#packages_type

doc/api/modules.md

+7-5
Original file line numberDiff line numberDiff line change
@@ -391,8 +391,8 @@ directories, and then provide a single entry point to those directories.
391391
There are three ways in which a folder may be passed to `require()` as
392392
an argument.
393393

394-
The first is to create a `package.json` file in the root of the folder,
395-
which specifies a `main` module. An example `package.json` file might
394+
The first is to create a [`package.json`][] file in the root of the folder,
395+
which specifies a `main` module. An example [`package.json`][] file might
396396
look like this:
397397

398398
```json
@@ -406,10 +406,10 @@ If this was in a folder at `./some-library`, then
406406

407407
This is the extent of the awareness of `package.json` files within Node.js.
408408

409-
If there is no `package.json` file present in the directory, or if the
410-
`'main'` entry is missing or cannot be resolved, then Node.js
409+
If there is no [`package.json`][] file present in the directory, or if the
410+
[`"main"`][] entry is missing or cannot be resolved, then Node.js
411411
will attempt to load an `index.js` or `index.node` file out of that
412-
directory. For example, if there was no `package.json` file in the above
412+
directory. For example, if there was no [`package.json`][] file in the above
413413
example, then `require('./some-library')` would attempt to load:
414414

415415
* `./some-library/index.js`
@@ -986,3 +986,5 @@ This section was moved to
986986
[module resolution]: #modules_all_together
987987
[native addons]: addons.html
988988
[`require.main`]: #modules_require_main
989+
[`package.json`]: packages.html#packages_package_json_supported_fields
990+
[`"main"`]: packages.html#packages_main

0 commit comments

Comments
 (0)