2020-08-07 13:04:13 +02:00
|
|
|
# Modules: Packages
|
|
|
|
|
2021-01-13 17:00:41 +01:00
|
|
|
<!--introduced_in=v12.20.0-->
|
2021-09-19 17:18:42 -07:00
|
|
|
|
2020-08-07 13:04:13 +02:00
|
|
|
<!-- type=misc -->
|
2021-09-19 17:18:42 -07:00
|
|
|
|
2020-09-28 13:23:53 +02:00
|
|
|
<!-- YAML
|
|
|
|
changes:
|
|
|
|
- version:
|
|
|
|
- v14.13.0
|
2020-11-16 12:04:38 -05:00
|
|
|
- v12.20.0
|
2020-09-28 13:23:53 +02:00
|
|
|
pr-url: https://github.com/nodejs/node/pull/34718
|
|
|
|
description: Add support for `"exports"` patterns.
|
2021-02-09 11:42:18 +01:00
|
|
|
- version:
|
|
|
|
- v14.6.0
|
|
|
|
- v12.19.0
|
2020-09-28 13:23:53 +02:00
|
|
|
pr-url: https://github.com/nodejs/node/pull/34117
|
|
|
|
description: Add package `"imports"` field.
|
2022-03-27 03:40:52 +11:00
|
|
|
- version:
|
|
|
|
- v13.7.0
|
|
|
|
- v12.17.0
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/29866
|
|
|
|
description: Unflag conditional exports.
|
2020-09-28 13:23:53 +02:00
|
|
|
- version:
|
|
|
|
- v13.7.0
|
|
|
|
- v12.16.0
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/31001
|
2022-03-27 03:40:52 +11:00
|
|
|
description: Remove the `--experimental-conditional-exports` option. In 12.16.0, conditional exports are still behind `--experimental-modules`.
|
2020-09-28 13:23:53 +02:00
|
|
|
- version:
|
|
|
|
- v13.6.0
|
|
|
|
- v12.16.0
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/31002
|
|
|
|
description: Unflag self-referencing a package using its name.
|
|
|
|
- version: v12.7.0
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/28568
|
|
|
|
description:
|
|
|
|
Introduce `"exports"` `package.json` field as a more powerful alternative
|
|
|
|
to the classic `"main"` field.
|
|
|
|
- version: v12.0.0
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/26745
|
|
|
|
description:
|
|
|
|
Add support for ES modules using `.js` file extension via `package.json`
|
|
|
|
`"type"` field.
|
|
|
|
-->
|
2020-08-07 13:04:13 +02:00
|
|
|
|
|
|
|
## Introduction
|
|
|
|
|
2020-09-26 22:13:02 -07:00
|
|
|
A package is a folder tree described by a `package.json` file. The package
|
|
|
|
consists of the folder containing the `package.json` file and all subfolders
|
|
|
|
until the next folder containing another `package.json` file, or a folder
|
|
|
|
named `node_modules`.
|
2020-08-07 13:04:13 +02:00
|
|
|
|
2020-09-26 22:13:02 -07:00
|
|
|
This page provides guidance for package authors writing `package.json` files
|
|
|
|
along with a reference for the [`package.json`][] fields defined by Node.js.
|
2020-08-07 13:04:13 +02:00
|
|
|
|
|
|
|
## Determining module system
|
|
|
|
|
2023-09-28 23:18:44 -07:00
|
|
|
### Introduction
|
|
|
|
|
2020-08-07 13:04:13 +02:00
|
|
|
Node.js will treat the following as [ES modules][] when passed to `node` as the
|
2022-01-17 11:36:19 +01:00
|
|
|
initial input, or when referenced by `import` statements or `import()`
|
|
|
|
expressions:
|
2020-08-07 13:04:13 +02:00
|
|
|
|
2022-01-17 11:36:19 +01:00
|
|
|
* Files with an `.mjs` extension.
|
2020-08-07 13:04:13 +02:00
|
|
|
|
2022-01-17 11:36:19 +01:00
|
|
|
* Files with a `.js` extension when the nearest parent `package.json` file
|
|
|
|
contains a top-level [`"type"`][] field with a value of `"module"`.
|
2020-08-07 13:04:13 +02:00
|
|
|
|
|
|
|
* Strings passed in as an argument to `--eval`, or piped to `node` via `STDIN`,
|
|
|
|
with the flag `--input-type=module`.
|
|
|
|
|
2024-07-20 11:30:46 -07:00
|
|
|
* Code containing syntax only successfully parsed as [ES modules][], such as
|
|
|
|
`import` or `export` statements or `import.meta`, with no explicit marker of
|
|
|
|
how it should be interpreted. Explicit markers are `.mjs` or `.cjs`
|
|
|
|
extensions, `package.json` `"type"` fields with either `"module"` or
|
2024-12-02 14:43:55 -08:00
|
|
|
`"commonjs"` values, or the `--input-type` flag. Dynamic `import()`
|
|
|
|
expressions are supported in either CommonJS or ES modules and would not force
|
|
|
|
a file to be treated as an ES module. See [Syntax detection][].
|
2023-10-20 08:44:56 -07:00
|
|
|
|
2023-09-28 23:18:44 -07:00
|
|
|
Node.js will treat the following as [CommonJS][] when passed to `node` as the
|
|
|
|
initial input, or when referenced by `import` statements or `import()`
|
|
|
|
expressions:
|
2020-08-07 13:04:13 +02:00
|
|
|
|
2022-01-17 11:36:19 +01:00
|
|
|
* Files with a `.cjs` extension.
|
2020-08-07 13:04:13 +02:00
|
|
|
|
2022-01-17 11:36:19 +01:00
|
|
|
* Files with a `.js` extension when the nearest parent `package.json` file
|
|
|
|
contains a top-level field [`"type"`][] with a value of `"commonjs"`.
|
2020-08-07 13:04:13 +02:00
|
|
|
|
|
|
|
* Strings passed in as an argument to `--eval` or `--print`, or piped to `node`
|
|
|
|
via `STDIN`, with the flag `--input-type=commonjs`.
|
|
|
|
|
2024-12-02 14:43:55 -08:00
|
|
|
* Files with a `.js` extension with no parent `package.json` file or where the
|
|
|
|
nearest parent `package.json` file lacks a `type` field, and where the code
|
|
|
|
can evaluate successfully as CommonJS. In other words, Node.js tries to run
|
|
|
|
such "ambiguous" files as CommonJS first, and will retry evaluating them as ES
|
|
|
|
modules if the evaluation as CommonJS fails because the parser found ES module
|
|
|
|
syntax.
|
|
|
|
|
|
|
|
Writing ES module syntax in "ambiguous" files incurs a performance cost, and
|
|
|
|
therefore it is encouraged that authors be explicit wherever possible. In
|
|
|
|
particular, package authors should always include the [`"type"`][] field in
|
|
|
|
their `package.json` files, even in packages where all sources are CommonJS.
|
|
|
|
Being explicit about the `type` of the package will future-proof the package in
|
|
|
|
case the default type of Node.js ever changes, and it will also make things
|
|
|
|
easier for build tools and loaders to determine how the files in the package
|
|
|
|
should be interpreted.
|
2020-08-07 13:04:13 +02:00
|
|
|
|
2024-07-20 11:30:46 -07:00
|
|
|
### Syntax detection
|
|
|
|
|
|
|
|
<!-- YAML
|
|
|
|
added:
|
|
|
|
- v21.1.0
|
|
|
|
- v20.10.0
|
|
|
|
changes:
|
|
|
|
- version:
|
2024-08-19 11:58:36 -03:00
|
|
|
- v22.7.0
|
2025-03-06 13:18:40 +01:00
|
|
|
- v20.19.0
|
2024-07-20 11:30:46 -07:00
|
|
|
pr-url: https://github.com/nodejs/node/pull/53619
|
|
|
|
description: Syntax detection is enabled by default.
|
|
|
|
-->
|
|
|
|
|
|
|
|
> Stability: 1.2 - Release candidate
|
|
|
|
|
|
|
|
Node.js will inspect the source code of ambiguous input to determine whether it
|
|
|
|
contains ES module syntax; if such syntax is detected, the input will be treated
|
|
|
|
as an ES module.
|
|
|
|
|
|
|
|
Ambiguous input is defined as:
|
|
|
|
|
|
|
|
* Files with a `.js` extension or no extension; and either no controlling
|
2024-12-02 14:43:55 -08:00
|
|
|
`package.json` file or one that lacks a `type` field.
|
|
|
|
* String input (`--eval` or `STDIN`) when `--input-type`is not specified.
|
2024-07-20 11:30:46 -07:00
|
|
|
|
|
|
|
ES module syntax is defined as syntax that would throw when evaluated as
|
|
|
|
CommonJS. This includes the following:
|
|
|
|
|
|
|
|
* `import` statements (but _not_ `import()` expressions, which are valid in
|
|
|
|
CommonJS).
|
|
|
|
* `export` statements.
|
|
|
|
* `import.meta` references.
|
|
|
|
* `await` at the top level of a module.
|
|
|
|
* Lexical redeclarations of the CommonJS wrapper variables (`require`, `module`,
|
|
|
|
`exports`, `__dirname`, `__filename`).
|
|
|
|
|
2022-01-17 11:36:19 +01:00
|
|
|
### Modules loaders
|
|
|
|
|
|
|
|
Node.js has two systems for resolving a specifier and loading modules.
|
|
|
|
|
|
|
|
There is the CommonJS module loader:
|
|
|
|
|
|
|
|
* It is fully synchronous.
|
|
|
|
* It is responsible for handling `require()` calls.
|
|
|
|
* It is monkey patchable.
|
|
|
|
* It supports [folders as modules][].
|
|
|
|
* When resolving a specifier, if no exact match is found, it will try to add
|
|
|
|
extensions (`.js`, `.json`, and finally `.node`) and then attempt to resolve
|
|
|
|
[folders as modules][].
|
|
|
|
* It treats `.json` as JSON text files.
|
|
|
|
* `.node` files are interpreted as compiled addon modules loaded with
|
|
|
|
`process.dlopen()`.
|
|
|
|
* It treats all files that lack `.json` or `.node` extensions as JavaScript
|
|
|
|
text files.
|
2024-09-29 23:15:15 +10:00
|
|
|
* It can only be used to [load ECMAScript modules from CommonJS modules][] if
|
2024-09-26 16:21:37 +02:00
|
|
|
the module graph is synchronous (that contains no top-level `await`).
|
module: support require()ing synchronous ESM graphs
This patch adds `require()` support for synchronous ESM graphs under
the flag `--experimental-require-module`
This is based on the the following design aspect of ESM:
- The resolution can be synchronous (up to the host)
- The evaluation of a synchronous graph (without top-level await) is
also synchronous, and, by the time the module graph is instantiated
(before evaluation starts), this is is already known.
If `--experimental-require-module` is enabled, and the ECMAScript
module being loaded by `require()` meets the following requirements:
- Explicitly marked as an ES module with a `"type": "module"` field in
the closest package.json or a `.mjs` extension.
- Fully synchronous (contains no top-level `await`).
`require()` will load the requested module as an ES Module, and return
the module name space object. In this case it is similar to dynamic
`import()` but is run synchronously and returns the name space object
directly.
```mjs
// point.mjs
export function distance(a, b) {
return (b.x - a.x) ** 2 + (b.y - a.y) ** 2;
}
class Point {
constructor(x, y) { this.x = x; this.y = y; }
}
export default Point;
```
```cjs
const required = require('./point.mjs');
// [Module: null prototype] {
// default: [class Point],
// distance: [Function: distance]
// }
console.log(required);
(async () => {
const imported = await import('./point.mjs');
console.log(imported === required); // true
})();
```
If the module being `require()`'d contains top-level `await`, or the
module graph it `import`s contains top-level `await`,
[`ERR_REQUIRE_ASYNC_MODULE`][] will be thrown. In this case, users
should load the asynchronous module using `import()`.
If `--experimental-print-required-tla` is enabled, instead of throwing
`ERR_REQUIRE_ASYNC_MODULE` before evaluation, Node.js will evaluate the
module, try to locate the top-level awaits, and print their location to
help users fix them.
PR-URL: https://github.com/nodejs/node/pull/51977
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Guy Bedford <guybedford@gmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
2024-03-12 01:50:24 +08:00
|
|
|
When used to load a JavaScript text file that is not an ECMAScript module,
|
|
|
|
the file will be loaded as a CommonJS module.
|
2022-01-17 11:36:19 +01:00
|
|
|
|
|
|
|
There is the ECMAScript module loader:
|
|
|
|
|
module: support require()ing synchronous ESM graphs
This patch adds `require()` support for synchronous ESM graphs under
the flag `--experimental-require-module`
This is based on the the following design aspect of ESM:
- The resolution can be synchronous (up to the host)
- The evaluation of a synchronous graph (without top-level await) is
also synchronous, and, by the time the module graph is instantiated
(before evaluation starts), this is is already known.
If `--experimental-require-module` is enabled, and the ECMAScript
module being loaded by `require()` meets the following requirements:
- Explicitly marked as an ES module with a `"type": "module"` field in
the closest package.json or a `.mjs` extension.
- Fully synchronous (contains no top-level `await`).
`require()` will load the requested module as an ES Module, and return
the module name space object. In this case it is similar to dynamic
`import()` but is run synchronously and returns the name space object
directly.
```mjs
// point.mjs
export function distance(a, b) {
return (b.x - a.x) ** 2 + (b.y - a.y) ** 2;
}
class Point {
constructor(x, y) { this.x = x; this.y = y; }
}
export default Point;
```
```cjs
const required = require('./point.mjs');
// [Module: null prototype] {
// default: [class Point],
// distance: [Function: distance]
// }
console.log(required);
(async () => {
const imported = await import('./point.mjs');
console.log(imported === required); // true
})();
```
If the module being `require()`'d contains top-level `await`, or the
module graph it `import`s contains top-level `await`,
[`ERR_REQUIRE_ASYNC_MODULE`][] will be thrown. In this case, users
should load the asynchronous module using `import()`.
If `--experimental-print-required-tla` is enabled, instead of throwing
`ERR_REQUIRE_ASYNC_MODULE` before evaluation, Node.js will evaluate the
module, try to locate the top-level awaits, and print their location to
help users fix them.
PR-URL: https://github.com/nodejs/node/pull/51977
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Guy Bedford <guybedford@gmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
2024-03-12 01:50:24 +08:00
|
|
|
* It is asynchronous, unless it's being used to load modules for `require()`.
|
2022-01-17 11:36:19 +01:00
|
|
|
* It is responsible for handling `import` statements and `import()` expressions.
|
|
|
|
* It is not monkey patchable, can be customized using [loader hooks][].
|
|
|
|
* It does not support folders as modules, directory indexes (e.g.
|
|
|
|
`'./startup/index.js'`) must be fully specified.
|
|
|
|
* It does no extension searching. A file extension must be provided
|
|
|
|
when the specifier is a relative or absolute file URL.
|
2024-05-17 10:07:48 +02:00
|
|
|
* It can load JSON modules, but an import type attribute is required.
|
2022-01-17 11:36:19 +01:00
|
|
|
* It accepts only `.js`, `.mjs`, and `.cjs` extensions for JavaScript text
|
|
|
|
files.
|
|
|
|
* It can be used to load JavaScript CommonJS modules. Such modules
|
2022-08-21 02:15:37 +08:00
|
|
|
are passed through the `cjs-module-lexer` to try to identify named exports,
|
2022-01-17 11:36:19 +01:00
|
|
|
which are available if they can be determined through static analysis.
|
|
|
|
Imported CommonJS modules have their URLs converted to absolute
|
|
|
|
paths and are then loaded via the CommonJS module loader.
|
|
|
|
|
2020-09-26 22:13:02 -07:00
|
|
|
### `package.json` and file extensions
|
2020-08-07 13:04:13 +02:00
|
|
|
|
2020-09-26 22:13:02 -07:00
|
|
|
Within a package, the [`package.json`][] [`"type"`][] field defines how
|
2020-08-07 13:12:25 +02:00
|
|
|
Node.js should interpret `.js` files. If a `package.json` file does not have a
|
|
|
|
`"type"` field, `.js` files are treated as [CommonJS][].
|
|
|
|
|
|
|
|
A `package.json` `"type"` value of `"module"` tells Node.js to interpret `.js`
|
2020-09-26 22:13:02 -07:00
|
|
|
files within that package as using [ES module][] syntax.
|
2020-08-07 13:04:13 +02:00
|
|
|
|
2020-09-26 22:13:02 -07:00
|
|
|
The `"type"` field applies not only to initial entry points (`node my-app.js`)
|
2020-08-07 13:04:13 +02:00
|
|
|
but also to files referenced by `import` statements and `import()` expressions.
|
|
|
|
|
|
|
|
```js
|
2020-09-26 22:13:02 -07:00
|
|
|
// my-app.js, treated as an ES module because there is a package.json
|
2020-08-07 13:04:13 +02:00
|
|
|
// file in the same folder with "type": "module".
|
|
|
|
|
|
|
|
import './startup/init.js';
|
|
|
|
// Loaded as ES module since ./startup contains no package.json file,
|
2020-09-26 22:13:02 -07:00
|
|
|
// and therefore inherits the "type" value from one level up.
|
2020-08-07 13:04:13 +02:00
|
|
|
|
|
|
|
import 'commonjs-package';
|
|
|
|
// Loaded as CommonJS since ./node_modules/commonjs-package/package.json
|
|
|
|
// lacks a "type" field or contains "type": "commonjs".
|
|
|
|
|
|
|
|
import './node_modules/commonjs-package/index.js';
|
|
|
|
// Loaded as CommonJS since ./node_modules/commonjs-package/package.json
|
|
|
|
// lacks a "type" field or contains "type": "commonjs".
|
|
|
|
```
|
|
|
|
|
|
|
|
Files ending with `.mjs` are always loaded as [ES modules][] regardless of
|
2020-09-26 22:13:02 -07:00
|
|
|
the nearest parent `package.json`.
|
2020-08-07 13:04:13 +02:00
|
|
|
|
2020-09-26 22:13:02 -07:00
|
|
|
Files ending with `.cjs` are always loaded as [CommonJS][] regardless of the
|
|
|
|
nearest parent `package.json`.
|
2020-08-07 13:04:13 +02:00
|
|
|
|
|
|
|
```js
|
|
|
|
import './legacy-file.cjs';
|
|
|
|
// Loaded as CommonJS since .cjs is always loaded as CommonJS.
|
|
|
|
|
|
|
|
import 'commonjs-package/src/index.mjs';
|
|
|
|
// Loaded as ES module since .mjs is always loaded as ES module.
|
|
|
|
```
|
|
|
|
|
2020-09-30 05:33:26 -07:00
|
|
|
The `.mjs` and `.cjs` extensions can be used to mix types within the same
|
2020-09-26 22:13:02 -07:00
|
|
|
package:
|
2020-08-07 13:04:13 +02:00
|
|
|
|
2020-09-26 22:13:02 -07:00
|
|
|
* Within a `"type": "module"` package, Node.js can be instructed to
|
2020-08-07 13:04:13 +02:00
|
|
|
interpret a particular file as [CommonJS][] by naming it with a `.cjs`
|
|
|
|
extension (since both `.js` and `.mjs` files are treated as ES modules within
|
2020-09-26 22:13:02 -07:00
|
|
|
a `"module"` package).
|
2020-08-07 13:04:13 +02:00
|
|
|
|
2020-09-26 22:13:02 -07:00
|
|
|
* Within a `"type": "commonjs"` package, Node.js can be instructed to
|
2020-08-07 13:12:25 +02:00
|
|
|
interpret a particular file as an [ES module][] by naming it with an `.mjs`
|
2020-08-07 13:04:13 +02:00
|
|
|
extension (since both `.js` and `.cjs` files are treated as CommonJS within a
|
2020-09-26 22:13:02 -07:00
|
|
|
`"commonjs"` package).
|
2020-08-07 13:04:13 +02:00
|
|
|
|
|
|
|
### `--input-type` flag
|
2021-10-10 21:55:04 -07:00
|
|
|
|
2021-02-09 11:42:18 +01:00
|
|
|
<!-- YAML
|
|
|
|
added: v12.0.0
|
|
|
|
-->
|
2020-08-07 13:04:13 +02:00
|
|
|
|
|
|
|
Strings passed in as an argument to `--eval` (or `-e`), or piped to `node` via
|
2020-09-30 05:33:26 -07:00
|
|
|
`STDIN`, are treated as [ES modules][] when the `--input-type=module` flag
|
2020-08-07 13:04:13 +02:00
|
|
|
is set.
|
|
|
|
|
|
|
|
```bash
|
2022-04-20 10:23:41 +02:00
|
|
|
node --input-type=module --eval "import { sep } from 'node:path'; console.log(sep);"
|
2020-08-07 13:04:13 +02:00
|
|
|
|
2022-04-20 10:23:41 +02:00
|
|
|
echo "import { sep } from 'node:path'; console.log(sep);" | node --input-type=module
|
2020-08-07 13:04:13 +02:00
|
|
|
```
|
|
|
|
|
|
|
|
For completeness there is also `--input-type=commonjs`, for explicitly running
|
|
|
|
string input as CommonJS. This is the default behavior if `--input-type` is
|
|
|
|
unspecified.
|
|
|
|
|
|
|
|
## Package entry points
|
|
|
|
|
2022-05-17 21:04:51 +02:00
|
|
|
In a package's `package.json` file, two fields can define entry points for a
|
2022-06-10 18:38:58 -07:00
|
|
|
package: [`"main"`][] and [`"exports"`][]. Both fields apply to both ES module
|
|
|
|
and CommonJS module entry points.
|
|
|
|
|
|
|
|
The [`"main"`][] field is supported in all versions of Node.js, but its
|
|
|
|
capabilities are limited: it only defines the main entry point of the package.
|
|
|
|
|
|
|
|
The [`"exports"`][] provides a modern alternative to [`"main"`][] allowing
|
|
|
|
multiple entry points to be defined, conditional entry resolution support
|
|
|
|
between environments, and **preventing any other entry points besides those
|
|
|
|
defined in [`"exports"`][]**. This encapsulation allows module authors to
|
|
|
|
clearly define the public interface for their package.
|
|
|
|
|
|
|
|
For new packages targeting the currently supported versions of Node.js, the
|
|
|
|
[`"exports"`][] field is recommended. For packages supporting Node.js 10 and
|
|
|
|
below, the [`"main"`][] field is required. If both [`"exports"`][] and
|
|
|
|
[`"main"`][] are defined, the [`"exports"`][] field takes precedence over
|
|
|
|
[`"main"`][] in supported versions of Node.js.
|
2020-08-07 13:04:13 +02:00
|
|
|
|
2020-08-07 13:12:25 +02:00
|
|
|
[Conditional exports][] can be used within [`"exports"`][] to define different
|
2020-08-07 13:04:13 +02:00
|
|
|
package entry points per environment, including whether the package is
|
|
|
|
referenced via `require` or via `import`. For more information about supporting
|
2022-06-10 18:38:58 -07:00
|
|
|
both CommonJS and ES modules in a single package please consult
|
2020-08-07 13:04:13 +02:00
|
|
|
[the dual CommonJS/ES module packages section][].
|
|
|
|
|
2022-06-10 18:38:58 -07:00
|
|
|
Existing packages introducing the [`"exports"`][] field will prevent consumers
|
|
|
|
of the package from using any entry points that are not defined, including the
|
2023-09-07 14:28:59 +02:00
|
|
|
[`package.json`][] (e.g. `require('your-package/package.json')`). **This will
|
2020-08-07 13:12:25 +02:00
|
|
|
likely be a breaking change.**
|
2020-08-07 13:04:13 +02:00
|
|
|
|
2020-08-07 13:12:25 +02:00
|
|
|
To make the introduction of [`"exports"`][] non-breaking, ensure that every
|
2020-08-07 13:04:13 +02:00
|
|
|
previously supported entry point is exported. It is best to explicitly specify
|
2022-05-17 21:04:51 +02:00
|
|
|
entry points so that the package's public API is well-defined. For example,
|
2022-07-27 15:55:06 +09:00
|
|
|
a project that previously exported `main`, `lib`,
|
2020-08-07 13:04:13 +02:00
|
|
|
`feature`, and the `package.json` could use the following `package.exports`:
|
|
|
|
|
|
|
|
```json
|
|
|
|
{
|
2022-06-10 18:38:58 -07:00
|
|
|
"name": "my-package",
|
2020-08-07 13:04:13 +02:00
|
|
|
"exports": {
|
|
|
|
".": "./lib/index.js",
|
|
|
|
"./lib": "./lib/index.js",
|
|
|
|
"./lib/index": "./lib/index.js",
|
|
|
|
"./lib/index.js": "./lib/index.js",
|
|
|
|
"./feature": "./feature/index.js",
|
2022-06-10 18:38:58 -07:00
|
|
|
"./feature/index": "./feature/index.js",
|
2020-08-07 13:04:13 +02:00
|
|
|
"./feature/index.js": "./feature/index.js",
|
|
|
|
"./package.json": "./package.json"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2022-06-10 18:38:58 -07:00
|
|
|
Alternatively a project could choose to export entire folders both with and
|
|
|
|
without extensioned subpaths using export patterns:
|
2020-08-07 13:04:13 +02:00
|
|
|
|
|
|
|
```json
|
|
|
|
{
|
2022-06-10 18:38:58 -07:00
|
|
|
"name": "my-package",
|
2020-08-07 13:04:13 +02:00
|
|
|
"exports": {
|
|
|
|
".": "./lib/index.js",
|
|
|
|
"./lib": "./lib/index.js",
|
2020-08-11 18:40:05 -07:00
|
|
|
"./lib/*": "./lib/*.js",
|
2022-06-10 18:38:58 -07:00
|
|
|
"./lib/*.js": "./lib/*.js",
|
2020-08-07 13:04:13 +02:00
|
|
|
"./feature": "./feature/index.js",
|
2020-08-11 18:40:05 -07:00
|
|
|
"./feature/*": "./feature/*.js",
|
2022-06-10 18:38:58 -07:00
|
|
|
"./feature/*.js": "./feature/*.js",
|
2020-08-07 13:04:13 +02:00
|
|
|
"./package.json": "./package.json"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2022-06-10 18:38:58 -07:00
|
|
|
With the above providing backwards-compatibility for any minor package versions,
|
|
|
|
a future major change for the package can then properly restrict the exports
|
|
|
|
to only the specific feature exports exposed:
|
|
|
|
|
|
|
|
```json
|
|
|
|
{
|
|
|
|
"name": "my-package",
|
|
|
|
"exports": {
|
|
|
|
".": "./lib/index.js",
|
|
|
|
"./feature/*.js": "./feature/*.js",
|
|
|
|
"./feature/internal/*": null
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
2020-08-07 13:04:13 +02:00
|
|
|
|
|
|
|
### Main entry point export
|
|
|
|
|
2022-06-10 18:38:58 -07:00
|
|
|
When writing a new package, it is recommended to use the [`"exports"`][] field:
|
2020-08-07 13:04:13 +02:00
|
|
|
|
|
|
|
```json
|
|
|
|
{
|
2022-06-10 18:38:58 -07:00
|
|
|
"exports": "./index.js"
|
2020-08-07 13:04:13 +02:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2020-09-30 05:33:26 -07:00
|
|
|
When the [`"exports"`][] field is defined, all subpaths of the package are
|
2020-09-26 22:13:02 -07:00
|
|
|
encapsulated and no longer available to importers. For example,
|
2020-09-30 05:33:26 -07:00
|
|
|
`require('pkg/subpath.js')` throws an [`ERR_PACKAGE_PATH_NOT_EXPORTED`][]
|
2020-09-26 22:13:02 -07:00
|
|
|
error.
|
2020-08-07 13:04:13 +02:00
|
|
|
|
|
|
|
This encapsulation of exports provides more reliable guarantees
|
|
|
|
about package interfaces for tools and when handling semver upgrades for a
|
|
|
|
package. It is not a strong encapsulation since a direct require of any
|
|
|
|
absolute subpath of the package such as
|
|
|
|
`require('/path/to/node_modules/pkg/subpath.js')` will still load `subpath.js`.
|
|
|
|
|
2022-06-10 18:38:58 -07:00
|
|
|
All currently supported versions of Node.js and modern build tools support the
|
|
|
|
`"exports"` field. For projects using an older version of Node.js or a related
|
|
|
|
build tool, compatibility can be achieved by including the `"main"` field
|
|
|
|
alongside `"exports"` pointing to the same module:
|
|
|
|
|
|
|
|
```json
|
|
|
|
{
|
|
|
|
"main": "./index.js",
|
|
|
|
"exports": "./index.js"
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2020-08-07 13:04:13 +02:00
|
|
|
### Subpath exports
|
2021-10-10 21:55:04 -07:00
|
|
|
|
2021-02-09 11:42:18 +01:00
|
|
|
<!-- YAML
|
|
|
|
added: v12.7.0
|
|
|
|
-->
|
2020-08-07 13:04:13 +02:00
|
|
|
|
2020-08-07 13:12:25 +02:00
|
|
|
When using the [`"exports"`][] field, custom subpaths can be defined along
|
2020-08-07 13:04:13 +02:00
|
|
|
with the main entry point by treating the main entry point as the
|
|
|
|
`"."` subpath:
|
|
|
|
|
|
|
|
```json
|
|
|
|
{
|
|
|
|
"exports": {
|
2022-06-10 18:38:58 -07:00
|
|
|
".": "./index.js",
|
|
|
|
"./submodule.js": "./src/submodule.js"
|
2020-08-07 13:04:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2020-08-07 13:12:25 +02:00
|
|
|
Now only the defined subpath in [`"exports"`][] can be imported by a consumer:
|
2020-08-07 13:04:13 +02:00
|
|
|
|
|
|
|
```js
|
2022-06-10 18:38:58 -07:00
|
|
|
import submodule from 'es-module-package/submodule.js';
|
2020-08-07 13:04:13 +02:00
|
|
|
// Loads ./node_modules/es-module-package/src/submodule.js
|
|
|
|
```
|
|
|
|
|
|
|
|
While other subpaths will error:
|
|
|
|
|
|
|
|
```js
|
|
|
|
import submodule from 'es-module-package/private-module.js';
|
|
|
|
// Throws ERR_PACKAGE_PATH_NOT_EXPORTED
|
|
|
|
```
|
|
|
|
|
2022-06-10 18:38:58 -07:00
|
|
|
#### Extensions in subpaths
|
|
|
|
|
|
|
|
Package authors should provide either extensioned (`import 'pkg/subpath.js'`) or
|
|
|
|
extensionless (`import 'pkg/subpath'`) subpaths in their exports. This ensures
|
|
|
|
that there is only one subpath for each exported module so that all dependents
|
|
|
|
import the same consistent specifier, keeping the package contract clear for
|
|
|
|
consumers and simplifying package subpath completions.
|
|
|
|
|
|
|
|
Traditionally, packages tended to use the extensionless style, which has the
|
|
|
|
benefits of readability and of masking the true path of the file within the
|
|
|
|
package.
|
|
|
|
|
|
|
|
With [import maps][] now providing a standard for package resolution in browsers
|
|
|
|
and other JavaScript runtimes, using the extensionless style can result in
|
|
|
|
bloated import map definitions. Explicit file extensions can avoid this issue by
|
|
|
|
enabling the import map to utilize a [packages folder mapping][] to map multiple
|
|
|
|
subpaths where possible instead of a separate map entry per package subpath
|
|
|
|
export. This also mirrors the requirement of using [the full specifier path][]
|
|
|
|
in relative and absolute import specifiers.
|
|
|
|
|
|
|
|
### Exports sugar
|
|
|
|
|
|
|
|
<!-- YAML
|
|
|
|
added: v12.11.0
|
|
|
|
-->
|
|
|
|
|
|
|
|
If the `"."` export is the only export, the [`"exports"`][] field provides sugar
|
|
|
|
for this case being the direct [`"exports"`][] field value.
|
|
|
|
|
|
|
|
```json
|
|
|
|
{
|
|
|
|
"exports": {
|
|
|
|
".": "./index.js"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
can be written:
|
|
|
|
|
|
|
|
```json
|
|
|
|
{
|
|
|
|
"exports": "./index.js"
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2020-10-07 00:55:10 -04:00
|
|
|
### Subpath imports
|
2021-10-10 21:55:04 -07:00
|
|
|
|
2021-04-21 10:54:38 +10:00
|
|
|
<!-- YAML
|
2021-02-09 11:42:18 +01:00
|
|
|
added:
|
|
|
|
- v14.6.0
|
|
|
|
- v12.19.0
|
|
|
|
-->
|
2020-08-11 18:40:05 -07:00
|
|
|
|
2022-06-10 18:38:58 -07:00
|
|
|
In addition to the [`"exports"`][] field, there is a package `"imports"` field
|
|
|
|
to create private mappings that only apply to import specifiers from within the
|
|
|
|
package itself.
|
2020-10-07 00:55:10 -04:00
|
|
|
|
2022-06-10 18:38:58 -07:00
|
|
|
Entries in the `"imports"` field must always start with `#` to ensure they are
|
|
|
|
disambiguated from external package specifiers.
|
2020-10-07 00:55:10 -04:00
|
|
|
|
|
|
|
For example, the imports field can be used to gain the benefits of conditional
|
|
|
|
exports for internal modules:
|
|
|
|
|
|
|
|
```json
|
|
|
|
// package.json
|
|
|
|
{
|
|
|
|
"imports": {
|
|
|
|
"#dep": {
|
|
|
|
"node": "dep-node-native",
|
|
|
|
"default": "./dep-polyfill.js"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"dependencies": {
|
|
|
|
"dep-node-native": "^1.0.0"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
where `import '#dep'` does not get the resolution of the external package
|
|
|
|
`dep-node-native` (including its exports in turn), and instead gets the local
|
|
|
|
file `./dep-polyfill.js` relative to the package in other environments.
|
|
|
|
|
|
|
|
Unlike the `"exports"` field, the `"imports"` field permits mapping to external
|
|
|
|
packages.
|
|
|
|
|
2022-06-10 18:38:58 -07:00
|
|
|
The resolution rules for the imports field are otherwise analogous to the
|
|
|
|
exports field.
|
2020-10-07 00:55:10 -04:00
|
|
|
|
|
|
|
### Subpath patterns
|
2021-10-10 21:55:04 -07:00
|
|
|
|
2021-04-21 10:54:38 +10:00
|
|
|
<!-- YAML
|
2021-02-09 11:42:18 +01:00
|
|
|
added:
|
|
|
|
- v14.13.0
|
2021-04-22 16:52:49 +02:00
|
|
|
- v12.20.0
|
2022-06-10 18:38:58 -07:00
|
|
|
changes:
|
|
|
|
- version:
|
|
|
|
- v16.10.0
|
|
|
|
- v14.19.0
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/40041
|
|
|
|
description: Support pattern trailers in "imports" field.
|
|
|
|
- version:
|
|
|
|
- v16.9.0
|
|
|
|
- v14.19.0
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/39635
|
|
|
|
description: Support pattern trailers.
|
2021-02-09 11:42:18 +01:00
|
|
|
-->
|
2020-10-07 00:55:10 -04:00
|
|
|
|
|
|
|
For packages with a small number of exports or imports, we recommend
|
|
|
|
explicitly listing each exports subpath entry. But for packages that have
|
|
|
|
large numbers of subpaths, this might cause `package.json` bloat and
|
|
|
|
maintenance issues.
|
2020-08-11 18:40:05 -07:00
|
|
|
|
|
|
|
For these use cases, subpath export patterns can be used instead:
|
2020-08-07 13:04:13 +02:00
|
|
|
|
|
|
|
```json
|
|
|
|
// ./node_modules/es-module-package/package.json
|
|
|
|
{
|
|
|
|
"exports": {
|
2022-06-10 18:38:58 -07:00
|
|
|
"./features/*.js": "./src/features/*.js"
|
2020-10-07 00:55:10 -04:00
|
|
|
},
|
|
|
|
"imports": {
|
2022-06-10 18:38:58 -07:00
|
|
|
"#internal/*.js": "./src/internal/*.js"
|
2020-08-07 13:04:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2021-05-18 18:39:06 +02:00
|
|
|
**`*` maps expose nested subpaths as it is a string replacement syntax
|
|
|
|
only.**
|
|
|
|
|
2021-08-02 14:46:23 -07:00
|
|
|
All instances of `*` on the right hand side will then be replaced with this
|
|
|
|
value, including if it contains any `/` separators.
|
2020-08-07 13:04:13 +02:00
|
|
|
|
|
|
|
```js
|
2022-06-10 18:38:58 -07:00
|
|
|
import featureX from 'es-module-package/features/x.js';
|
2020-08-07 13:04:13 +02:00
|
|
|
// Loads ./node_modules/es-module-package/src/features/x.js
|
2020-08-11 18:40:05 -07:00
|
|
|
|
2022-06-10 18:38:58 -07:00
|
|
|
import featureY from 'es-module-package/features/y/y.js';
|
2020-08-11 18:40:05 -07:00
|
|
|
// Loads ./node_modules/es-module-package/src/features/y/y.js
|
2020-10-07 00:55:10 -04:00
|
|
|
|
2022-06-10 18:38:58 -07:00
|
|
|
import internalZ from '#internal/z.js';
|
2020-10-07 00:55:10 -04:00
|
|
|
// Loads ./node_modules/es-module-package/src/internal/z.js
|
2020-08-07 13:04:13 +02:00
|
|
|
```
|
|
|
|
|
2022-06-10 18:38:58 -07:00
|
|
|
This is a direct static matching and replacement without any special handling
|
|
|
|
for file extensions. Including the `"*.js"` on both sides of the mapping
|
|
|
|
restricts the exposed package exports to only JS files.
|
2020-08-11 18:40:05 -07:00
|
|
|
|
|
|
|
The property of exports being statically enumerable is maintained with exports
|
|
|
|
patterns since the individual exports for a package can be determined by
|
|
|
|
treating the right hand side target pattern as a `**` glob against the list of
|
|
|
|
files within the package. Because `node_modules` paths are forbidden in exports
|
|
|
|
targets, this expansion is dependent on only the files of the package itself.
|
2020-08-07 13:04:13 +02:00
|
|
|
|
2021-05-18 18:39:06 +02:00
|
|
|
To exclude private subfolders from patterns, `null` targets can be used:
|
|
|
|
|
|
|
|
```json
|
|
|
|
// ./node_modules/es-module-package/package.json
|
|
|
|
{
|
|
|
|
"exports": {
|
2022-06-10 18:38:58 -07:00
|
|
|
"./features/*.js": "./src/features/*.js",
|
2021-05-18 18:39:06 +02:00
|
|
|
"./features/private-internal/*": null
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
```js
|
2022-06-10 18:38:58 -07:00
|
|
|
import featureInternal from 'es-module-package/features/private-internal/m.js';
|
2021-05-18 18:39:06 +02:00
|
|
|
// Throws: ERR_PACKAGE_PATH_NOT_EXPORTED
|
|
|
|
|
2022-06-10 18:38:58 -07:00
|
|
|
import featureX from 'es-module-package/features/x.js';
|
2021-05-18 18:39:06 +02:00
|
|
|
// Loads ./node_modules/es-module-package/src/features/x.js
|
|
|
|
```
|
|
|
|
|
2020-08-07 13:04:13 +02:00
|
|
|
### Conditional exports
|
2021-10-10 21:55:04 -07:00
|
|
|
|
2021-04-21 10:54:38 +10:00
|
|
|
<!-- YAML
|
2021-02-09 11:42:18 +01:00
|
|
|
added:
|
|
|
|
- v13.2.0
|
|
|
|
- v12.16.0
|
|
|
|
changes:
|
|
|
|
- version:
|
|
|
|
- v13.7.0
|
|
|
|
- v12.16.0
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/31001
|
|
|
|
description: Unflag conditional exports.
|
|
|
|
-->
|
2020-08-07 13:04:13 +02:00
|
|
|
|
|
|
|
Conditional exports provide a way to map to different paths depending on
|
|
|
|
certain conditions. They are supported for both CommonJS and ES module imports.
|
|
|
|
|
|
|
|
For example, a package that wants to provide different ES module exports for
|
|
|
|
`require()` and `import` can be written:
|
|
|
|
|
|
|
|
```json
|
|
|
|
// package.json
|
|
|
|
{
|
|
|
|
"exports": {
|
2022-06-10 18:38:58 -07:00
|
|
|
"import": "./index-module.js",
|
|
|
|
"require": "./index-require.cjs"
|
2020-08-07 13:04:13 +02:00
|
|
|
},
|
|
|
|
"type": "module"
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2021-11-02 21:41:41 -07:00
|
|
|
Node.js implements the following conditions, listed in order from most
|
|
|
|
specific to least specific as conditions should be defined:
|
2020-08-07 13:04:13 +02:00
|
|
|
|
2021-11-02 21:41:41 -07:00
|
|
|
* `"node-addons"` - similar to `"node"` and matches for any Node.js environment.
|
|
|
|
This condition can be used to provide an entry point which uses native C++
|
|
|
|
addons as opposed to an entry point which is more universal and doesn't rely
|
|
|
|
on native addons. This condition can be disabled via the
|
|
|
|
[`--no-addons` flag][].
|
|
|
|
* `"node"` - matches for any Node.js environment. Can be a CommonJS or ES
|
|
|
|
module file. _In most cases explicitly calling out the Node.js platform is
|
|
|
|
not necessary._
|
2020-09-30 05:33:26 -07:00
|
|
|
* `"import"` - matches when the package is loaded via `import` or
|
2021-10-10 21:55:04 -07:00
|
|
|
`import()`, or via any top-level import or resolve operation by the
|
|
|
|
ECMAScript module loader. Applies regardless of the module format of the
|
|
|
|
target file. _Always mutually exclusive with `"require"`._
|
2020-09-30 05:33:26 -07:00
|
|
|
* `"require"` - matches when the package is loaded via `require()`. The
|
2021-10-10 21:55:04 -07:00
|
|
|
referenced file should be loadable with `require()` although the condition
|
|
|
|
matches regardless of the module format of the target file. Expected
|
2024-09-26 16:21:37 +02:00
|
|
|
formats include CommonJS, JSON, native addons, and ES modules. _Always mutually
|
module: support require()ing synchronous ESM graphs
This patch adds `require()` support for synchronous ESM graphs under
the flag `--experimental-require-module`
This is based on the the following design aspect of ESM:
- The resolution can be synchronous (up to the host)
- The evaluation of a synchronous graph (without top-level await) is
also synchronous, and, by the time the module graph is instantiated
(before evaluation starts), this is is already known.
If `--experimental-require-module` is enabled, and the ECMAScript
module being loaded by `require()` meets the following requirements:
- Explicitly marked as an ES module with a `"type": "module"` field in
the closest package.json or a `.mjs` extension.
- Fully synchronous (contains no top-level `await`).
`require()` will load the requested module as an ES Module, and return
the module name space object. In this case it is similar to dynamic
`import()` but is run synchronously and returns the name space object
directly.
```mjs
// point.mjs
export function distance(a, b) {
return (b.x - a.x) ** 2 + (b.y - a.y) ** 2;
}
class Point {
constructor(x, y) { this.x = x; this.y = y; }
}
export default Point;
```
```cjs
const required = require('./point.mjs');
// [Module: null prototype] {
// default: [class Point],
// distance: [Function: distance]
// }
console.log(required);
(async () => {
const imported = await import('./point.mjs');
console.log(imported === required); // true
})();
```
If the module being `require()`'d contains top-level `await`, or the
module graph it `import`s contains top-level `await`,
[`ERR_REQUIRE_ASYNC_MODULE`][] will be thrown. In this case, users
should load the asynchronous module using `import()`.
If `--experimental-print-required-tla` is enabled, instead of throwing
`ERR_REQUIRE_ASYNC_MODULE` before evaluation, Node.js will evaluate the
module, try to locate the top-level awaits, and print their location to
help users fix them.
PR-URL: https://github.com/nodejs/node/pull/51977
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Guy Bedford <guybedford@gmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
2024-03-12 01:50:24 +08:00
|
|
|
exclusive with `"import"`._
|
module: implement the "module-sync" exports condition
This patch implements a "module-sync" exports condition
for packages to supply a sycnrhonous ES module to the
Node.js module loader, no matter it's being required
or imported. This is similar to the "module" condition
that bundlers have been using to support `require(esm)`
in Node.js, and allows dual-package authors to opt into
ESM-first only newer versions of Node.js that supports
require(esm) while avoiding the dual-package hazard.
```json
{
"type": "module",
"exports": {
"node": {
// On new version of Node.js, both require() and import get
// the ESM version
"module-sync": "./index.js",
// On older version of Node.js, where "module" and
// require(esm) are not supported, use the transpiled CJS version
// to avoid dual-package hazard. Library authors can decide
// to drop support for older versions of Node.js when they think
// it's time.
"default": "./dist/index.cjs"
},
// On any other environment, use the ESM version.
"default": "./index.js"
}
}
```
We end up implementing a condition with a different name
instead of reusing "module", because existing code in the
ecosystem using the "module" condition sometimes also expect
the module resolution for these ESM files to work in CJS
style, which is supported by bundlers, but the native
Node.js loader has intentionally made ESM resolution
different from CJS resolution (e.g. forbidding `import
'./noext'` or `import './directory'`), so it would be
semver-major to implement a `"module"` condition
without implementing the forbidden ESM resolution rules.
For now, this just implments a new condition as semver-minor
so it can be backported to older LTS.
Refs: https://webpack.js.org/guides/package-exports/#target-environment-independent-packages
PR-URL: https://github.com/nodejs/node/pull/54648
Fixes: https://github.com/nodejs/node/issues/52173
Refs: https://github.com/joyeecheung/test-module-condition
Refs: https://github.com/nodejs/node/issues/52697
Reviewed-By: Jacob Smith <jacob@frende.me>
Reviewed-By: Jan Krems <jan.krems@gmail.com>
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
2024-09-25 08:35:26 +02:00
|
|
|
* `"module-sync"` - matches no matter the package is loaded via `import`,
|
|
|
|
`import()` or `require()`. The format is expected to be ES modules that does
|
|
|
|
not contain top-level await in its module graph - if it does,
|
|
|
|
`ERR_REQUIRE_ASYNC_MODULE` will be thrown when the module is `require()`-ed.
|
2020-09-30 05:33:26 -07:00
|
|
|
* `"default"` - the generic fallback that always matches. Can be a CommonJS
|
2021-10-10 21:55:04 -07:00
|
|
|
or ES module file. _This condition should always come last._
|
2020-08-07 13:04:13 +02:00
|
|
|
|
2020-08-07 13:12:25 +02:00
|
|
|
Within the [`"exports"`][] object, key order is significant. During condition
|
2020-08-07 13:04:13 +02:00
|
|
|
matching, earlier entries have higher priority and take precedence over later
|
|
|
|
entries. _The general rule is that conditions should be from most specific to
|
|
|
|
least specific in object order_.
|
|
|
|
|
|
|
|
Using the `"import"` and `"require"` conditions can lead to some hazards,
|
|
|
|
which are further explained in [the dual CommonJS/ES module packages section][].
|
|
|
|
|
2021-11-02 21:41:41 -07:00
|
|
|
The `"node-addons"` condition can be used to provide an entry point which
|
|
|
|
uses native C++ addons. However, this condition can be disabled via the
|
|
|
|
[`--no-addons` flag][]. When using `"node-addons"`, it's recommended to treat
|
|
|
|
`"default"` as an enhancement that provides a more universal entry point, e.g.
|
|
|
|
using WebAssembly instead of a native addon.
|
|
|
|
|
2020-08-07 13:04:13 +02:00
|
|
|
Conditional exports can also be extended to exports subpaths, for example:
|
|
|
|
|
|
|
|
```json
|
|
|
|
{
|
|
|
|
"exports": {
|
2022-06-10 18:38:58 -07:00
|
|
|
".": "./index.js",
|
|
|
|
"./feature.js": {
|
2020-08-07 13:04:13 +02:00
|
|
|
"node": "./feature-node.js",
|
|
|
|
"default": "./feature.js"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2022-06-10 18:38:58 -07:00
|
|
|
Defines a package where `require('pkg/feature.js')` and
|
|
|
|
`import 'pkg/feature.js'` could provide different implementations between
|
|
|
|
Node.js and other JS environments.
|
2020-08-07 13:04:13 +02:00
|
|
|
|
|
|
|
When using environment branches, always include a `"default"` condition where
|
|
|
|
possible. Providing a `"default"` condition ensures that any unknown JS
|
|
|
|
environments are able to use this universal implementation, which helps avoid
|
|
|
|
these JS environments from having to pretend to be existing environments in
|
|
|
|
order to support packages with conditional exports. For this reason, using
|
|
|
|
`"node"` and `"default"` condition branches is usually preferable to using
|
|
|
|
`"node"` and `"browser"` condition branches.
|
|
|
|
|
|
|
|
### Nested conditions
|
|
|
|
|
|
|
|
In addition to direct mappings, Node.js also supports nested condition objects.
|
|
|
|
|
|
|
|
For example, to define a package that only has dual mode entry points for
|
|
|
|
use in Node.js but not the browser:
|
|
|
|
|
|
|
|
```json
|
|
|
|
{
|
|
|
|
"exports": {
|
|
|
|
"node": {
|
|
|
|
"import": "./feature-node.mjs",
|
|
|
|
"require": "./feature-node.cjs"
|
|
|
|
},
|
2021-12-17 18:35:09 +01:00
|
|
|
"default": "./feature.mjs"
|
2020-08-07 13:04:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
Conditions continue to be matched in order as with flat conditions. If
|
2021-09-27 13:07:16 +05:00
|
|
|
a nested condition does not have any mapping it will continue checking
|
2020-08-07 13:04:13 +02:00
|
|
|
the remaining conditions of the parent condition. In this way nested
|
|
|
|
conditions behave analogously to nested JavaScript `if` statements.
|
|
|
|
|
|
|
|
### Resolving user conditions
|
2021-10-10 21:55:04 -07:00
|
|
|
|
2021-02-09 11:42:18 +01:00
|
|
|
<!-- YAML
|
|
|
|
added:
|
|
|
|
- v14.9.0
|
|
|
|
- v12.19.0
|
|
|
|
-->
|
2020-08-07 13:04:13 +02:00
|
|
|
|
|
|
|
When running Node.js, custom user conditions can be added with the
|
|
|
|
`--conditions` flag:
|
|
|
|
|
|
|
|
```bash
|
2022-06-10 18:38:58 -07:00
|
|
|
node --conditions=development index.js
|
2020-08-07 13:04:13 +02:00
|
|
|
```
|
|
|
|
|
|
|
|
which would then resolve the `"development"` condition in package imports and
|
2021-09-02 15:17:42 +02:00
|
|
|
exports, while resolving the existing `"node"`, `"node-addons"`, `"default"`,
|
|
|
|
`"import"`, and `"require"` conditions as appropriate.
|
2020-08-07 13:04:13 +02:00
|
|
|
|
|
|
|
Any number of custom conditions can be set with repeat flags.
|
|
|
|
|
2024-09-30 11:08:35 -07:00
|
|
|
Typical conditions should only contain alphanumerical characters,
|
|
|
|
using ":", "-", or "=" as separators if necessary. Anything else may run
|
|
|
|
into compability issues outside of node.
|
|
|
|
|
|
|
|
In node, conditions have very few restrictions, but specifically these include:
|
|
|
|
|
|
|
|
1. They must contain at least one character.
|
|
|
|
2. They cannot start with "." since they may appear in places that also
|
|
|
|
allow relative paths.
|
|
|
|
3. They cannot contain "," since they may be parsed as a comma-separated
|
|
|
|
list by some CLI tools.
|
|
|
|
4. They cannot be integer property keys like "10" since that can have
|
|
|
|
unexpected effects on property key ordering for JS objects.
|
|
|
|
|
2021-11-02 21:41:41 -07:00
|
|
|
### Community Conditions Definitions
|
2021-01-09 13:58:14 -05:00
|
|
|
|
module: implement the "module-sync" exports condition
This patch implements a "module-sync" exports condition
for packages to supply a sycnrhonous ES module to the
Node.js module loader, no matter it's being required
or imported. This is similar to the "module" condition
that bundlers have been using to support `require(esm)`
in Node.js, and allows dual-package authors to opt into
ESM-first only newer versions of Node.js that supports
require(esm) while avoiding the dual-package hazard.
```json
{
"type": "module",
"exports": {
"node": {
// On new version of Node.js, both require() and import get
// the ESM version
"module-sync": "./index.js",
// On older version of Node.js, where "module" and
// require(esm) are not supported, use the transpiled CJS version
// to avoid dual-package hazard. Library authors can decide
// to drop support for older versions of Node.js when they think
// it's time.
"default": "./dist/index.cjs"
},
// On any other environment, use the ESM version.
"default": "./index.js"
}
}
```
We end up implementing a condition with a different name
instead of reusing "module", because existing code in the
ecosystem using the "module" condition sometimes also expect
the module resolution for these ESM files to work in CJS
style, which is supported by bundlers, but the native
Node.js loader has intentionally made ESM resolution
different from CJS resolution (e.g. forbidding `import
'./noext'` or `import './directory'`), so it would be
semver-major to implement a `"module"` condition
without implementing the forbidden ESM resolution rules.
For now, this just implments a new condition as semver-minor
so it can be backported to older LTS.
Refs: https://webpack.js.org/guides/package-exports/#target-environment-independent-packages
PR-URL: https://github.com/nodejs/node/pull/54648
Fixes: https://github.com/nodejs/node/issues/52173
Refs: https://github.com/joyeecheung/test-module-condition
Refs: https://github.com/nodejs/node/issues/52697
Reviewed-By: Jacob Smith <jacob@frende.me>
Reviewed-By: Jan Krems <jan.krems@gmail.com>
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
2024-09-25 08:35:26 +02:00
|
|
|
Condition strings other than the `"import"`, `"require"`, `"node"`, `"module-sync"`,
|
2021-11-02 21:41:41 -07:00
|
|
|
`"node-addons"` and `"default"` conditions
|
|
|
|
[implemented in Node.js core](#conditional-exports) are ignored by default.
|
2021-01-09 13:58:14 -05:00
|
|
|
|
2021-11-02 21:41:41 -07:00
|
|
|
Other platforms may implement other conditions and user conditions can be
|
|
|
|
enabled in Node.js via the [`--conditions` / `-C` flag][].
|
2021-01-09 13:58:14 -05:00
|
|
|
|
2021-11-02 21:41:41 -07:00
|
|
|
Since custom package conditions require clear definitions to ensure correct
|
|
|
|
usage, a list of common known package conditions and their strict definitions
|
|
|
|
is provided below to assist with ecosystem coordination.
|
2021-01-09 13:58:14 -05:00
|
|
|
|
2021-11-02 21:41:41 -07:00
|
|
|
* `"types"` - can be used by typing systems to resolve the typing file for
|
|
|
|
the given export. _This condition should always be included first._
|
|
|
|
* `"browser"` - any web browser environment.
|
2021-01-09 13:58:14 -05:00
|
|
|
* `"development"` - can be used to define a development-only environment
|
2021-11-02 21:41:41 -07:00
|
|
|
entry point, for example to provide additional debugging context such as
|
|
|
|
better error messages when running in a development mode. _Must always be
|
|
|
|
mutually exclusive with `"production"`._
|
2021-01-09 13:58:14 -05:00
|
|
|
* `"production"` - can be used to define a production environment entry
|
2021-10-10 21:55:04 -07:00
|
|
|
point. _Must always be mutually exclusive with `"development"`._
|
2021-01-09 13:58:14 -05:00
|
|
|
|
2023-06-19 21:24:27 -05:00
|
|
|
For other runtimes, platform-specific condition key definitions are maintained
|
|
|
|
by the [WinterCG][] in the [Runtime Keys][] proposal specification.
|
|
|
|
|
2021-06-30 21:35:50 -07:00
|
|
|
New conditions definitions may be added to this list by creating a pull request
|
|
|
|
to the [Node.js documentation for this section][]. The requirements for listing
|
|
|
|
a new condition definition here are that:
|
2021-01-09 13:58:14 -05:00
|
|
|
|
2021-02-27 23:51:57 +05:30
|
|
|
* The definition should be clear and unambiguous for all implementers.
|
2021-01-09 13:58:14 -05:00
|
|
|
* The use case for why the condition is needed should be clearly justified.
|
|
|
|
* There should exist sufficient existing implementation usage.
|
|
|
|
* The condition name should not conflict with another condition definition or
|
|
|
|
condition in wide usage.
|
|
|
|
* The listing of the condition definition should provide a coordination
|
|
|
|
benefit to the ecosystem that wouldn't otherwise be possible. For example,
|
|
|
|
this would not necessarily be the case for company-specific or
|
|
|
|
application-specific conditions.
|
2023-06-19 21:24:27 -05:00
|
|
|
* The condition should be such that a Node.js user would expect it to be in
|
|
|
|
Node.js core documentation. The `"types"` condition is a good example: It
|
|
|
|
doesn't really belong in the [Runtime Keys][] proposal but is a good fit
|
|
|
|
here in the Node.js docs.
|
2021-01-09 13:58:14 -05:00
|
|
|
|
|
|
|
The above definitions may be moved to a dedicated conditions registry in due
|
|
|
|
course.
|
|
|
|
|
2020-08-07 13:04:13 +02:00
|
|
|
### Self-referencing a package using its name
|
2021-10-10 21:55:04 -07:00
|
|
|
|
2021-04-21 10:54:38 +10:00
|
|
|
<!-- YAML
|
2021-02-09 11:42:18 +01:00
|
|
|
added:
|
|
|
|
- v13.1.0
|
|
|
|
- v12.16.0
|
|
|
|
changes:
|
|
|
|
- version:
|
|
|
|
- v13.6.0
|
|
|
|
- v12.16.0
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/31002
|
|
|
|
description: Unflag self-referencing a package using its name.
|
|
|
|
-->
|
2020-08-07 13:04:13 +02:00
|
|
|
|
2022-05-17 21:04:51 +02:00
|
|
|
Within a package, the values defined in the package's
|
|
|
|
`package.json` [`"exports"`][] field can be referenced via the package's name.
|
2020-08-07 13:04:13 +02:00
|
|
|
For example, assuming the `package.json` is:
|
|
|
|
|
|
|
|
```json
|
|
|
|
// package.json
|
|
|
|
{
|
|
|
|
"name": "a-package",
|
|
|
|
"exports": {
|
2022-06-10 18:38:58 -07:00
|
|
|
".": "./index.mjs",
|
|
|
|
"./foo.js": "./foo.js"
|
2020-08-07 13:04:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
Then any module _in that package_ can reference an export in the package itself:
|
|
|
|
|
|
|
|
```js
|
|
|
|
// ./a-module.mjs
|
2022-06-10 18:38:58 -07:00
|
|
|
import { something } from 'a-package'; // Imports "something" from ./index.mjs.
|
2020-08-07 13:04:13 +02:00
|
|
|
```
|
|
|
|
|
2020-08-07 13:12:25 +02:00
|
|
|
Self-referencing is available only if `package.json` has [`"exports"`][], and
|
|
|
|
will allow importing only what that [`"exports"`][] (in the `package.json`)
|
|
|
|
allows. So the code below, given the previous package, will generate a runtime
|
|
|
|
error:
|
2020-08-07 13:04:13 +02:00
|
|
|
|
|
|
|
```js
|
|
|
|
// ./another-module.mjs
|
|
|
|
|
|
|
|
// Imports "another" from ./m.mjs. Fails because
|
|
|
|
// the "package.json" "exports" field
|
|
|
|
// does not provide an export named "./m.mjs".
|
|
|
|
import { another } from 'a-package/m.mjs';
|
|
|
|
```
|
|
|
|
|
|
|
|
Self-referencing is also available when using `require`, both in an ES module,
|
|
|
|
and in a CommonJS one. For example, this code will also work:
|
|
|
|
|
2021-06-01 15:10:32 +02:00
|
|
|
```cjs
|
2020-08-07 13:04:13 +02:00
|
|
|
// ./a-module.js
|
2022-06-10 18:38:58 -07:00
|
|
|
const { something } = require('a-package/foo.js'); // Loads from ./foo.js.
|
2020-08-07 13:04:13 +02:00
|
|
|
```
|
|
|
|
|
2021-03-06 13:53:50 +01:00
|
|
|
Finally, self-referencing also works with scoped packages. For example, this
|
|
|
|
code will also work:
|
|
|
|
|
|
|
|
```json
|
|
|
|
// package.json
|
|
|
|
{
|
|
|
|
"name": "@my/package",
|
|
|
|
"exports": "./index.js"
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2021-08-17 06:39:38 -07:00
|
|
|
```cjs
|
2021-03-06 13:53:50 +01:00
|
|
|
// ./index.js
|
|
|
|
module.exports = 42;
|
|
|
|
```
|
|
|
|
|
2021-08-17 06:39:38 -07:00
|
|
|
```cjs
|
2021-03-06 13:53:50 +01:00
|
|
|
// ./other.js
|
|
|
|
console.log(require('@my/package'));
|
|
|
|
```
|
|
|
|
|
|
|
|
```console
|
|
|
|
$ node other.js
|
|
|
|
42
|
|
|
|
```
|
|
|
|
|
2020-08-07 13:04:13 +02:00
|
|
|
## Dual CommonJS/ES module packages
|
|
|
|
|
2024-10-22 22:14:50 +02:00
|
|
|
See [the package examples repository][] for details.
|
2020-08-07 13:04:13 +02:00
|
|
|
|
2020-08-07 13:12:25 +02:00
|
|
|
## Node.js `package.json` field definitions
|
|
|
|
|
|
|
|
This section describes the fields used by the Node.js runtime. Other tools (such
|
2022-01-28 00:35:23 +01:00
|
|
|
as [npm](https://docs.npmjs.com/cli/v8/configuring-npm/package-json)) use
|
2020-08-07 13:12:25 +02:00
|
|
|
additional fields which are ignored by Node.js and not documented here.
|
|
|
|
|
2020-09-26 22:13:02 -07:00
|
|
|
The following fields in `package.json` files are used in Node.js:
|
|
|
|
|
|
|
|
* [`"name"`][] - Relevant when using named imports within a package. Also used
|
|
|
|
by package managers as the name of the package.
|
2020-10-21 10:59:11 -04:00
|
|
|
* [`"main"`][] - The default module when loading the package, if exports is not
|
|
|
|
specified, and in versions of Node.js prior to the introduction of exports.
|
2020-09-26 22:13:02 -07:00
|
|
|
* [`"type"`][] - The package type determining whether to load `.js` files as
|
|
|
|
CommonJS or ES modules.
|
|
|
|
* [`"exports"`][] - Package exports and conditional exports. When present,
|
2020-09-30 05:33:26 -07:00
|
|
|
limits which submodules can be loaded from within the package.
|
2020-09-26 22:13:02 -07:00
|
|
|
* [`"imports"`][] - Package imports, for use by modules within the package
|
|
|
|
itself.
|
|
|
|
|
2020-08-07 13:12:25 +02:00
|
|
|
### `"name"`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
2020-08-07 13:12:25 +02:00
|
|
|
<!-- YAML
|
|
|
|
added:
|
|
|
|
- v13.1.0
|
2020-10-01 20:23:33 +02:00
|
|
|
- v12.16.0
|
2020-08-07 13:12:25 +02:00
|
|
|
changes:
|
|
|
|
- version:
|
|
|
|
- v13.6.0
|
2020-10-01 20:23:33 +02:00
|
|
|
- v12.16.0
|
2020-08-07 13:12:25 +02:00
|
|
|
pr-url: https://github.com/nodejs/node/pull/31002
|
|
|
|
description: Remove the `--experimental-resolve-self` option.
|
|
|
|
-->
|
|
|
|
|
|
|
|
* Type: {string}
|
|
|
|
|
|
|
|
```json
|
|
|
|
{
|
|
|
|
"name": "package-name"
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2022-05-17 21:04:51 +02:00
|
|
|
The `"name"` field defines your package's name. Publishing to the
|
2020-09-30 05:33:26 -07:00
|
|
|
_npm_ registry requires a name that satisfies
|
2020-08-07 13:12:25 +02:00
|
|
|
[certain requirements](https://docs.npmjs.com/files/package.json#name).
|
|
|
|
|
|
|
|
The `"name"` field can be used in addition to the [`"exports"`][] field to
|
|
|
|
[self-reference][] a package using its name.
|
|
|
|
|
2020-10-21 10:59:11 -04:00
|
|
|
### `"main"`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
2020-10-21 10:59:11 -04:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.4.0
|
|
|
|
-->
|
|
|
|
|
|
|
|
* Type: {string}
|
|
|
|
|
|
|
|
```json
|
|
|
|
{
|
2022-06-10 18:38:58 -07:00
|
|
|
"main": "./index.js"
|
2020-10-21 10:59:11 -04:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2022-01-30 06:26:22 -08:00
|
|
|
The `"main"` field defines the entry point of a package when imported by name
|
|
|
|
via a `node_modules` lookup. Its value is a path.
|
|
|
|
|
|
|
|
When a package has an [`"exports"`][] field, this will take precedence over the
|
|
|
|
`"main"` field when importing the package by name.
|
|
|
|
|
|
|
|
It also defines the script that is used when the [package directory is loaded
|
|
|
|
via `require()`](modules.md#folders-as-modules).
|
2020-10-21 10:59:11 -04:00
|
|
|
|
2021-06-01 15:10:32 +02:00
|
|
|
```cjs
|
2022-06-10 18:38:58 -07:00
|
|
|
// This resolves to ./path/to/directory/index.js.
|
|
|
|
require('./path/to/directory');
|
2020-10-21 10:59:11 -04:00
|
|
|
```
|
|
|
|
|
2020-08-07 13:12:25 +02:00
|
|
|
### `"type"`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
2020-08-07 13:12:25 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v12.0.0
|
|
|
|
changes:
|
|
|
|
- version:
|
|
|
|
- v13.2.0
|
2020-10-01 20:23:33 +02:00
|
|
|
- v12.17.0
|
2020-08-07 13:12:25 +02:00
|
|
|
pr-url: https://github.com/nodejs/node/pull/29866
|
|
|
|
description: Unflag `--experimental-modules`.
|
|
|
|
-->
|
|
|
|
|
|
|
|
* Type: {string}
|
|
|
|
|
2020-09-30 05:33:26 -07:00
|
|
|
The `"type"` field defines the module format that Node.js uses for all
|
2020-09-26 22:13:02 -07:00
|
|
|
`.js` files that have that `package.json` file as their nearest parent.
|
2020-08-07 13:12:25 +02:00
|
|
|
|
2020-09-30 05:33:26 -07:00
|
|
|
Files ending with `.js` are loaded as ES modules when the nearest parent
|
2020-08-07 13:12:25 +02:00
|
|
|
`package.json` file contains a top-level field `"type"` with a value of
|
|
|
|
`"module"`.
|
|
|
|
|
|
|
|
The nearest parent `package.json` is defined as the first `package.json` found
|
2022-05-17 21:04:51 +02:00
|
|
|
when searching in the current folder, that folder's parent, and so on up
|
2021-10-10 21:55:04 -07:00
|
|
|
until a node\_modules folder or the volume root is reached.
|
2020-08-07 13:12:25 +02:00
|
|
|
|
|
|
|
```json
|
|
|
|
// package.json
|
|
|
|
{
|
|
|
|
"type": "module"
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
```bash
|
|
|
|
# In same folder as preceding package.json
|
|
|
|
node my-app.js # Runs as ES module
|
|
|
|
```
|
|
|
|
|
|
|
|
If the nearest parent `package.json` lacks a `"type"` field, or contains
|
|
|
|
`"type": "commonjs"`, `.js` files are treated as [CommonJS][]. If the volume
|
2020-09-26 22:13:02 -07:00
|
|
|
root is reached and no `package.json` is found, `.js` files are treated as
|
|
|
|
[CommonJS][].
|
2020-08-07 13:12:25 +02:00
|
|
|
|
|
|
|
`import` statements of `.js` files are treated as ES modules if the nearest
|
|
|
|
parent `package.json` contains `"type": "module"`.
|
|
|
|
|
|
|
|
```js
|
|
|
|
// my-app.js, part of the same example as above
|
|
|
|
import './startup.js'; // Loaded as ES module because of package.json
|
|
|
|
```
|
|
|
|
|
|
|
|
Regardless of the value of the `"type"` field, `.mjs` files are always treated
|
|
|
|
as ES modules and `.cjs` files are always treated as CommonJS.
|
|
|
|
|
|
|
|
### `"exports"`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
2020-08-07 13:12:25 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v12.7.0
|
|
|
|
changes:
|
|
|
|
- version:
|
2020-10-01 20:49:03 +02:00
|
|
|
- v14.13.0
|
2021-04-22 16:52:49 +02:00
|
|
|
- v12.20.0
|
2020-10-01 20:49:03 +02:00
|
|
|
pr-url: https://github.com/nodejs/node/pull/34718
|
|
|
|
description: Add support for `"exports"` patterns.
|
2022-03-27 03:40:52 +11:00
|
|
|
- version:
|
|
|
|
- v13.7.0
|
|
|
|
- v12.17.0
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/29866
|
|
|
|
description: Unflag conditional exports.
|
2020-10-01 20:49:03 +02:00
|
|
|
- version:
|
|
|
|
- v13.7.0
|
2020-10-01 20:23:33 +02:00
|
|
|
- v12.16.0
|
2020-10-01 20:49:03 +02:00
|
|
|
pr-url: https://github.com/nodejs/node/pull/31008
|
|
|
|
description: Implement logical conditional exports ordering.
|
2020-08-07 13:12:25 +02:00
|
|
|
- version:
|
|
|
|
- v13.7.0
|
2020-10-01 20:23:33 +02:00
|
|
|
- v12.16.0
|
2020-08-07 13:12:25 +02:00
|
|
|
pr-url: https://github.com/nodejs/node/pull/31001
|
2022-03-27 03:40:52 +11:00
|
|
|
description: Remove the `--experimental-conditional-exports` option. In 12.16.0, conditional exports are still behind `--experimental-modules`.
|
2020-08-07 13:12:25 +02:00
|
|
|
- version:
|
2020-10-01 20:49:03 +02:00
|
|
|
- v13.2.0
|
2020-10-01 20:23:33 +02:00
|
|
|
- v12.16.0
|
2020-10-01 20:49:03 +02:00
|
|
|
pr-url: https://github.com/nodejs/node/pull/29978
|
|
|
|
description: Implement conditional exports.
|
2020-08-07 13:12:25 +02:00
|
|
|
-->
|
|
|
|
|
2021-10-10 21:55:04 -07:00
|
|
|
* Type: {Object} | {string} | {string\[]}
|
2020-08-07 13:12:25 +02:00
|
|
|
|
|
|
|
```json
|
|
|
|
{
|
|
|
|
"exports": "./index.js"
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
The `"exports"` field allows defining the [entry points][] of a package when
|
|
|
|
imported by name loaded either via a `node_modules` lookup or a
|
|
|
|
[self-reference][] to its own name. It is supported in Node.js 12+ as an
|
|
|
|
alternative to the [`"main"`][] that can support defining [subpath exports][]
|
|
|
|
and [conditional exports][] while encapsulating internal unexported modules.
|
|
|
|
|
|
|
|
[Conditional Exports][] can also be used within `"exports"` to define different
|
|
|
|
package entry points per environment, including whether the package is
|
|
|
|
referenced via `require` or via `import`.
|
|
|
|
|
|
|
|
All paths defined in the `"exports"` must be relative file URLs starting with
|
|
|
|
`./`.
|
|
|
|
|
|
|
|
### `"imports"`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
2020-08-07 13:12:25 +02:00
|
|
|
<!-- YAML
|
2021-04-22 17:05:58 +02:00
|
|
|
added:
|
|
|
|
- v14.6.0
|
|
|
|
- v12.19.0
|
2020-08-07 13:12:25 +02:00
|
|
|
-->
|
|
|
|
|
|
|
|
* Type: {Object}
|
|
|
|
|
|
|
|
```json
|
|
|
|
// package.json
|
|
|
|
{
|
|
|
|
"imports": {
|
|
|
|
"#dep": {
|
|
|
|
"node": "dep-node-native",
|
|
|
|
"default": "./dep-polyfill.js"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"dependencies": {
|
|
|
|
"dep-node-native": "^1.0.0"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2020-10-07 00:55:10 -04:00
|
|
|
Entries in the imports field must be strings starting with `#`.
|
2020-08-07 13:12:25 +02:00
|
|
|
|
2022-06-10 18:38:58 -07:00
|
|
|
Package imports permit mapping to external packages.
|
2020-08-07 13:12:25 +02:00
|
|
|
|
2020-10-07 00:55:10 -04:00
|
|
|
This field defines [subpath imports][] for the current package.
|
2020-08-07 13:12:25 +02:00
|
|
|
|
2020-09-14 17:09:13 +02:00
|
|
|
[CommonJS]: modules.md
|
2021-07-04 20:39:17 -07:00
|
|
|
[Conditional exports]: #conditional-exports
|
2020-09-14 17:09:13 +02:00
|
|
|
[ES module]: esm.md
|
2020-09-17 18:53:37 +02:00
|
|
|
[ES modules]: esm.md
|
2021-02-18 12:29:37 -05:00
|
|
|
[Node.js documentation for this section]: https://github.com/nodejs/node/blob/HEAD/doc/api/packages.md#conditions-definitions
|
2023-06-19 21:24:27 -05:00
|
|
|
[Runtime Keys]: https://runtime-keys.proposal.wintercg.org/
|
2024-07-20 11:30:46 -07:00
|
|
|
[Syntax detection]: #syntax-detection
|
2023-06-19 21:24:27 -05:00
|
|
|
[WinterCG]: https://wintercg.org/
|
2021-07-04 20:39:17 -07:00
|
|
|
[`"exports"`]: #exports
|
|
|
|
[`"imports"`]: #imports
|
|
|
|
[`"main"`]: #main
|
|
|
|
[`"name"`]: #name
|
|
|
|
[`"type"`]: #type
|
2021-11-02 21:41:41 -07:00
|
|
|
[`--conditions` / `-C` flag]: #resolving-user-conditions
|
2021-09-02 15:17:42 +02:00
|
|
|
[`--no-addons` flag]: cli.md#--no-addons
|
2021-07-04 20:39:17 -07:00
|
|
|
[`ERR_PACKAGE_PATH_NOT_EXPORTED`]: errors.md#err_package_path_not_exported
|
|
|
|
[`package.json`]: #nodejs-packagejson-field-definitions
|
|
|
|
[entry points]: #package-entry-points
|
2022-01-17 11:36:19 +01:00
|
|
|
[folders as modules]: modules.md#folders-as-modules
|
2022-06-10 18:38:58 -07:00
|
|
|
[import maps]: https://github.com/WICG/import-maps
|
2024-09-29 23:15:15 +10:00
|
|
|
[load ECMAScript modules from CommonJS modules]: modules.md#loading-ecmascript-modules-using-require
|
2022-01-17 11:36:19 +01:00
|
|
|
[loader hooks]: esm.md#loaders
|
2022-06-10 18:38:58 -07:00
|
|
|
[packages folder mapping]: https://github.com/WICG/import-maps#packages-via-trailing-slashes
|
2021-07-04 20:39:17 -07:00
|
|
|
[self-reference]: #self-referencing-a-package-using-its-name
|
|
|
|
[subpath exports]: #subpath-exports
|
|
|
|
[subpath imports]: #subpath-imports
|
|
|
|
[the dual CommonJS/ES module packages section]: #dual-commonjses-module-packages
|
|
|
|
[the full specifier path]: esm.md#mandatory-file-extensions
|
2024-10-22 22:14:50 +02:00
|
|
|
[the package examples repository]: https://github.com/nodejs/package-examples
|