Skip to content

Commit

Permalink
doc: document support for package.json fields
Browse files Browse the repository at this point in the history
  • Loading branch information
aduh95 committed Aug 29, 2020
1 parent dbc5c17 commit 539c33d
Show file tree
Hide file tree
Showing 4 changed files with 264 additions and 133 deletions.
22 changes: 12 additions & 10 deletions doc/api/errors.md
Original file line number Diff line number Diff line change
Expand Up @@ -1391,13 +1391,13 @@ An invalid or unknown file encoding was passed.
<a id="ERR_INVALID_PACKAGE_CONFIG"></a>
### `ERR_INVALID_PACKAGE_CONFIG`

An invalid `package.json` file was found which failed parsing.
An invalid [`package.json`][] file was found which failed parsing.

<a id="ERR_INVALID_PACKAGE_TARGET"></a>
### `ERR_INVALID_PACKAGE_TARGET`

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

<a id="ERR_INVALID_PERFORMANCE_MARK"></a>
### `ERR_INVALID_PERFORMANCE_MARK`
Expand Down Expand Up @@ -1730,15 +1730,16 @@ A given value is out of the accepted range.
<a id="ERR_PACKAGE_IMPORT_NOT_DEFINED"></a>
### `ERR_PACKAGE_IMPORT_NOT_DEFINED`

The `package.json` ["imports" field][] does not define the given internal
The [`package.json`][] [`"imports"`][] field does not define the given internal
package specifier mapping.

<a id="ERR_PACKAGE_PATH_NOT_EXPORTED"></a>
### `ERR_PACKAGE_PATH_NOT_EXPORTED`

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

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

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

<!-- eslint-skip -->
```js
Expand Down Expand Up @@ -2645,7 +2646,8 @@ closed.
[crypto digest algorithm]: crypto.html#crypto_crypto_gethashes
[domains]: domain.html
[event emitter-based]: events.html#events_class_eventemitter
[exports]: packages.html#packages_package_entry_points
[`package.json`]: packages.html#packages_package_json_supported_fields
[`"exports"`]: packages.html#packages_exports
[file descriptors]: https://en.wikipedia.org/wiki/File_descriptor
[policy]: policy.html
[RFC 7230 Section 3]: https://tools.ietf.org/html/rfc7230#section-3
Expand All @@ -2656,4 +2658,4 @@ closed.
[vm]: vm.html
[self-reference a package using its name]: packages.html#packages_self_referencing_a_package_using_its_name
[define a custom subpath]: packages.html#packages_subpath_exports
["imports" field]: packages.html#packages_internal_package_imports
[`"imports"`]: packages.html#packages_imports
23 changes: 13 additions & 10 deletions doc/api/esm.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ Expect major changes in the implementation including interoperability support,
specifier resolution, and default behavior.

<!-- Anchors to make sure old links find a target -->
<i id="#esm_package_json_type_field"></i>
<i id="#esm_packages_type_field"></i>
<i id="#esm_package_scope_and_file_extensions"></i>
<i id="#esm_input_type_flag"></i>

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

Node.js treats JavaScript code as CommonJS modules by default.
Authors can tell Node.js to treat JavaScript code as ECMAScript modules
via the `.mjs` file extension, the `package.json` `"type"` field, or the
via the `.mjs` file extension, the `package.json` [`"type"`][] field, or the
`--input-type` flag. See
[Modules: Packages](packages.html#packages_determining_module_system) for more
details.
Expand Down Expand Up @@ -253,9 +253,9 @@ can either be an URL-style relative path like `'./file.mjs'` or a package name
like `'fs'`.
Like in CommonJS, files within packages can be accessed by appending a path to
the package name; unless the package’s `package.json` contains an `"exports"`
field, in which case files within packages need to be accessed via the path
defined in `"exports"`.
the package name; unless the package’s [`package.json`][] contains an
[`"exports"`][] field, in which case files within packages need to be accessed
via the path defined in [`"exports"`][].
```js
import { sin, cos } from 'geometry/trigonometry-functions.mjs';
Expand Down Expand Up @@ -933,7 +933,7 @@ The resolver can throw the following errors:
> 1. If the folder at _packageURL_ does not exist, then
> 1. Set _parentURL_ to the parent URL path of _parentURL_.
> 1. Continue the next loop iteration.
> 1. Let _pjson_ be the result of **READ_PACKAGE_JSON**(_packageURL_).
> 1. Let _pjson_ be the result of **READ_packages**(_packageURL_).
> 1. If _pjson_ is not **null** and _pjson_._exports_ is not **null** or
> **undefined**, then
> 1. Let _exports_ be _pjson.exports_.
Expand All @@ -953,7 +953,7 @@ The resolver can throw the following errors:
> 1. Let _packageURL_ be the result of **READ_PACKAGE_SCOPE**(_parentURL_).
> 1. If _packageURL_ is **null**, then
> 1. Return **undefined**.
> 1. Let _pjson_ be the result of **READ_PACKAGE_JSON**(_packageURL_).
> 1. Let _pjson_ be the result of **READ_packages**(_packageURL_).
> 1. If _pjson_ is **null** or if _pjson_._exports_ is **null** or
> **undefined**, then
> 1. Return **undefined**.
Expand Down Expand Up @@ -995,7 +995,7 @@ The resolver can throw the following errors:
> 1. Throw an _Invalid Module Specifier_ error.
> 1. Let _packageURL_ be the result of **READ_PACKAGE_SCOPE**(_parentURL_).
> 1. If _packageURL_ is not **null**, then
> 1. Let _pjson_ be the result of **READ_PACKAGE_JSON**(_packageURL_).
> 1. Let _pjson_ be the result of **READ_packages**(_packageURL_).
> 1. If _pjson.imports_ is a non-null Object, then
> 1. Let _resolvedMatch_ be the result of
> **PACKAGE_IMPORTS_EXPORTS_RESOLVE**(_specifier_, _pjson.imports_,
Expand Down Expand Up @@ -1091,12 +1091,12 @@ _conditions_)
> 1. While _scopeURL_ is not the file system root,
> 1. Set _scopeURL_ to the parent URL of _scopeURL_.
> 1. If _scopeURL_ ends in a _"node_modules"_ path segment, return **null**.
> 1. Let _pjson_ be the result of **READ_PACKAGE_JSON**(_scopeURL_).
> 1. Let _pjson_ be the result of **READ_packages**(_scopeURL_).
> 1. If _pjson_ is not **null**, then
> 1. Return _pjson_.
> 1. Return **null**.

**READ_PACKAGE_JSON**(_packageURL_)
**READ_packages**(_packageURL_)

> 1. Let _pjsonURL_ be the resolution of _"package.json"_ within _packageURL_.
> 1. If the file at _pjsonURL_ does not exist, then
Expand Down Expand Up @@ -1159,3 +1159,6 @@ success!
[6.1.7 Array Index]: https://tc39.es/ecma262/#integer-index
[Top-Level Await]: https://github.com/tc39/proposal-top-level-await
[Core modules]: modules.html#modules_core_modules
[`package.json`]: packages.html#packages_package_json_supported_fields
[`"exports"`]: packages.html#packages_exports
[`"type"`]: packages.html#packages_type
12 changes: 7 additions & 5 deletions doc/api/modules.md
Original file line number Diff line number Diff line change
Expand Up @@ -391,8 +391,8 @@ directories, and then provide a single entry point to those directories.
There are three ways in which a folder may be passed to `require()` as
an argument.

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

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

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

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

* `./some-library/index.js`
Expand Down Expand Up @@ -986,3 +986,5 @@ This section was moved to
[module resolution]: #modules_all_together
[native addons]: addons.html
[`require.main`]: #modules_require_main
[`package.json`]: packages.html#packages_package_json_supported_fields
[`"main"`]: packages.html#packages_main
Loading

0 comments on commit 539c33d

Please sign in to comment.