Skip to content

Commit 1d1ce1f

Browse files
aduh95MylesBorins
authored andcommitted
doc: document support for package.json fields
Fixes: #33143 PR-URL: #34970 Reviewed-By: Guy Bedford <[email protected]> Reviewed-By: Geoffrey Booth <[email protected]> Reviewed-By: Trivikram Kamat <[email protected]>
1 parent ab7d0e9 commit 1d1ce1f

File tree

4 files changed

+282
-142
lines changed

4 files changed

+282
-142
lines changed

doc/api/errors.md

+9-8
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 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`
@@ -1712,13 +1712,13 @@ A given value is out of the accepted range.
17121712
<a id="ERR_PACKAGE_IMPORT_NOT_DEFINED"></a>
17131713
### `ERR_PACKAGE_IMPORT_NOT_DEFINED`
17141714

1715-
The `package.json` ["imports" field][] does not define the given internal
1715+
The `package.json` [`"imports"`][] field does not define the given internal
17161716
package specifier mapping.
17171717

17181718
<a id="ERR_PACKAGE_PATH_NOT_EXPORTED"></a>
17191719
### `ERR_PACKAGE_PATH_NOT_EXPORTED`
17201720

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

@@ -2074,7 +2074,7 @@ signal (such as [`subprocess.kill()`][]).
20742074

20752075
`import` a directory URL is unsupported. Instead,
20762076
[self-reference a package using its name][] and [define a custom subpath][] in
2077-
the `"exports"` field of the `package.json` file.
2077+
the [`"exports"`][] field of the [`package.json`][] file.
20782078

20792079
<!-- eslint-skip -->
20802080
```js
@@ -2560,7 +2560,8 @@ closed.
25602560
[crypto digest algorithm]: crypto.html#crypto_crypto_gethashes
25612561
[domains]: domain.html
25622562
[event emitter-based]: events.html#events_class_eventemitter
2563-
[exports]: packages.html#packages_package_entry_points
2563+
[`package.json`]: packages.html#packages_node_js_package_json_field_definitions
2564+
[`"exports"`]: packages.html#packages_exports
25642565
[file descriptors]: https://en.wikipedia.org/wiki/File_descriptor
25652566
[policy]: policy.html
25662567
[stream-based]: stream.html
@@ -2570,4 +2571,4 @@ closed.
25702571
[vm]: vm.html
25712572
[self-reference a package using its name]: packages.html#packages_self_referencing_a_package_using_its_name
25722573
[define a custom subpath]: packages.html#packages_subpath_exports
2573-
["imports" field]: packages.html#packages_internal_package_imports
2574+
[`"imports"`]: packages.html#packages_imports

doc/api/esm.md

+7-4
Original file line numberDiff line numberDiff line change
@@ -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';
@@ -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_node_js_package_json_field_definitions
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
@@ -390,8 +390,8 @@ directories, and then provide a single entry point to those directories.
390390
There are three ways in which a folder may be passed to `require()` as
391391
an argument.
392392

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

397397
```json
@@ -405,10 +405,10 @@ If this was in a folder at `./some-library`, then
405405

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

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

414414
* `./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_node_js_package_json_field_definitions
990+
[`"main"`]: packages.html#packages_main

0 commit comments

Comments
 (0)