2020-08-07 10:40:45 +02:00
|
|
|
# Modules: ECMAScript modules
|
2017-06-05 19:44:56 -05:00
|
|
|
|
2017-11-04 09:08:46 +01:00
|
|
|
<!--introduced_in=v8.5.0-->
|
2021-09-19 17:18:42 -07:00
|
|
|
|
2018-04-14 05:05:56 +03:00
|
|
|
<!-- type=misc -->
|
2021-09-19 17:18:42 -07:00
|
|
|
|
2020-09-28 13:23:53 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v8.5.0
|
|
|
|
changes:
|
2024-11-16 16:36:27 +00:00
|
|
|
- version:
|
|
|
|
- v23.1.0
|
2024-11-27 16:58:44 -05:00
|
|
|
- v22.12.0
|
2025-01-22 14:04:10 +01:00
|
|
|
- v20.18.3
|
2024-11-16 16:36:27 +00:00
|
|
|
- v18.20.5
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/55333
|
|
|
|
description: Import attributes are no longer experimental.
|
2024-11-18 16:58:59 +00:00
|
|
|
- version: v22.0.0
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/52104
|
|
|
|
description: Drop support for import assertions.
|
2023-11-12 08:52:51 +01:00
|
|
|
- version:
|
|
|
|
- v21.0.0
|
|
|
|
- v20.10.0
|
2024-03-20 17:34:55 +00:00
|
|
|
- v18.20.0
|
2023-10-14 05:52:38 +02:00
|
|
|
pr-url: https://github.com/nodejs/node/pull/50140
|
|
|
|
description: Add experimental support for import attributes.
|
2023-11-27 12:19:49 +01:00
|
|
|
- version:
|
|
|
|
- v20.0.0
|
|
|
|
- v18.19.0
|
2023-04-13 09:35:17 +02:00
|
|
|
pr-url: https://github.com/nodejs/node/pull/44710
|
2023-08-22 14:53:40 -07:00
|
|
|
description: Module customization hooks are executed off the main thread.
|
2022-05-04 17:51:12 +02:00
|
|
|
- version:
|
2022-07-12 10:15:11 +02:00
|
|
|
- v18.6.0
|
2022-08-16, Version 16.17.0 'Gallium' (LTS)
Notable changes:
Adds `util.parseArgs` helper for higher level command-line argument
parsing.
Contributed by Benjamin Coe, John Gee, Darcy Clarke, Joe Sepi,
Kevin Gibbons, Aaron Casanova, Jessica Nahulan, and Jordan Harband.
https://github.com/nodejs/node/pull/42675
Node.js ESM Loader hooks now support multiple custom loaders, and
composition is achieved via "chaining": `foo-loader` calls `bar-loader`
calls `qux-loader` (a custom loader _must_ now signal a short circuit
when intentionally not calling the next). See the ESM docs
(https://nodejs.org/dist/latest-v16.x/docs/api/esm.html) for details.
Contributed by Jacob Smith, Geoffrey Booth, and Bradley Farias.
https://github.com/nodejs/node/pull/42623
The `node:test` module, which was initially introduced in Node.js
v18.0.0, is now available with all the changes done to it up to Node.js
v18.7.0.
To better align Node.js' experimental implementation of the Web Crypto
API with other runtimes, several changes were made:
* Support for CFRG curves was added, with the `'Ed25519'`, `'Ed448'`,
`'X25519'`, and `'X448'` algorithms.
* The proprietary `'NODE-DSA'`, `'NODE-DH'`, `'NODE-SCRYPT'`,
`'NODE-ED25519'`, `'NODE-ED448'`, `'NODE-X25519'`, and `'NODE-X448'`
algorithms were removed.
* The proprietary `'node.keyObject'` import/export format was removed.
Contributed by Filip Skokan.
https://github.com/nodejs/node/pull/42507
https://github.com/nodejs/node/pull/43310
Updated Corepack to 0.12.1 - https://github.com/nodejs/node/pull/43965
Updated ICU to 71.1 - https://github.com/nodejs/node/pull/42655
Updated npm to 8.15.0 - https://github.com/nodejs/node/pull/43917
Updated Undici to 5.8.0 - https://github.com/nodejs/node/pull/43886
(SEMVER-MINOR) crypto: make authTagLength optional for CC20P1305 (Tobias Nießen) https://github.com/nodejs/node/pull/42427
(SEMVER-MINOR) crypto: align webcrypto RSA key import/export with other implementations (Filip Skokan) https://github.com/nodejs/node/pull/42816
(SEMVER-MINOR) dns: export error code constants from `dns/promises` (Feng Yu) https://github.com/nodejs/node/pull/43176
doc: deprecate coercion to integer in process.exit (Daeyeon Jeong) https://github.com/nodejs/node/pull/43738
(SEMVER-MINOR) doc: deprecate diagnostics_channel object subscribe method (Stephen Belanger) https://github.com/nodejs/node/pull/42714
(SEMVER-MINOR) errors: add support for cause in aborterror (James M Snell) https://github.com/nodejs/node/pull/41008
(SEMVER-MINOR) events: expose CustomEvent on global with CLI flag (Daeyeon Jeong) https://github.com/nodejs/node/pull/43885
(SEMVER-MINOR) events: add `CustomEvent` (Daeyeon Jeong) https://github.com/nodejs/node/pull/43514
(SEMVER-MINOR) events: propagate abortsignal reason in new AbortError ctor in events (James M Snell) https://github.com/nodejs/node/pull/41008
(SEMVER-MINOR) fs: propagate abortsignal reason in new AbortSignal constructors (James M Snell) https://github.com/nodejs/node/pull/41008
(SEMVER-MINOR) fs: make params in writing methods optional (LiviaMedeiros) https://github.com/nodejs/node/pull/42601
(SEMVER-MINOR) fs: add `read(buffer[, options])` versions (LiviaMedeiros) https://github.com/nodejs/node/pull/42768
(SEMVER-MINOR) http: add drop request event for http server (theanarkh) https://github.com/nodejs/node/pull/43806
(SEMVER-MINOR) http: add diagnostics channel for http client (theanarkh) https://github.com/nodejs/node/pull/43580
(SEMVER-MINOR) http: add perf_hooks detail for http request and client (theanarkh) https://github.com/nodejs/node/pull/43361
(SEMVER-MINOR) http: add uniqueHeaders option to request and createServer (Paolo Insogna) https://github.com/nodejs/node/pull/41397
(SEMVER-MINOR) http2: propagate abortsignal reason in new AbortError constructor (James M Snell) https://github.com/nodejs/node/pull/41008
(SEMVER-MINOR) http2: compat support for array headers (OneNail) https://github.com/nodejs/node/pull/42901
(SEMVER-MINOR) lib: propagate abortsignal reason in new AbortError constructor in blob (James M Snell) https://github.com/nodejs/node/pull/41008
(SEMVER-MINOR) lib: add abortSignal.throwIfAborted() (James M Snell) https://github.com/nodejs/node/pull/40951
(SEMVER-MINOR) lib: improved diagnostics_channel subscribe/unsubscribe (Stephen Belanger) https://github.com/nodejs/node/pull/42714
(SEMVER-MINOR) module: add isBuiltIn method (hemanth.hm) https://github.com/nodejs/node/pull/43396
(SEMVER-MINOR) module,repl: support 'node:'-only core modules (Colin Ihrig) https://github.com/nodejs/node/pull/42325
(SEMVER-MINOR) net: add drop event for net server (theanarkh) https://github.com/nodejs/node/pull/43582
(SEMVER-MINOR) net: add ability to reset a tcp socket (pupilTong) https://github.com/nodejs/node/pull/43112
(SEMVER-MINOR) node-api: emit uncaught-exception on unhandled tsfn callbacks (Chengzhong Wu) https://github.com/nodejs/node/pull/36510
(SEMVER-MINOR) perf_hooks: add PerformanceResourceTiming (RafaelGSS) https://github.com/nodejs/node/pull/42725
(SEMVER-MINOR) report: add more heap infos in process report (theanarkh) https://github.com/nodejs/node/pull/43116
(SEMVER-MINOR) src: add --openssl-legacy-provider option (Daniel Bevenius) https://github.com/nodejs/node/pull/40478
(SEMVER-MINOR) src: define fs.constants.S_IWUSR & S_IRUSR for Win (Liviu Ionescu) https://github.com/nodejs/node/pull/42757
(SEMVER-MINOR) src,doc,test: add --openssl-shared-config option (Daniel Bevenius) https://github.com/nodejs/node/pull/43124
(SEMVER-MINOR) stream: use cause options in AbortError constructors (James M Snell) https://github.com/nodejs/node/pull/41008
(SEMVER-MINOR) stream: add iterator helper find (Nitzan Uziely) https://github.com/nodejs/node/pull/41849
(SEMVER-MINOR) stream: add writableAborted (Robert Nagy) https://github.com/nodejs/node/pull/40802
(SEMVER-MINOR) timers: propagate signal.reason in awaitable timers (James M Snell) https://github.com/nodejs/node/pull/41008
(SEMVER-MINOR) v8: add v8.startupSnapshot utils (Joyee Cheung) https://github.com/nodejs/node/pull/43329
(SEMVER-MINOR) v8: export more fields in getHeapStatistics (theanarkh) https://github.com/nodejs/node/pull/42784
(SEMVER-MINOR) worker: add hasRef() to MessagePort (Darshan Sen) https://github.com/nodejs/node/pull/42849
PR-URL: https://github.com/nodejs/node/pull/44098
2022-08-02 14:34:18 +02:00
|
|
|
- v16.17.0
|
2022-05-04 17:51:12 +02:00
|
|
|
pr-url: https://github.com/nodejs/node/pull/42623
|
2023-08-22 14:53:40 -07:00
|
|
|
description: Add support for chaining module customization hooks.
|
2022-02-01 00:34:51 -05:00
|
|
|
- version:
|
|
|
|
- v17.1.0
|
|
|
|
- v16.14.0
|
2021-08-28 01:47:49 +02:00
|
|
|
pr-url: https://github.com/nodejs/node/pull/40250
|
2023-10-14 05:52:38 +02:00
|
|
|
description: Add experimental support for import assertions.
|
2021-08-25 22:55:14 +02:00
|
|
|
- version:
|
2021-10-19, Version 17.0.0 (Current)
Notable Changes:
Deprecations and Removals:
- (SEMVER-MAJOR) dns: runtime deprecate type coercion of `dns.lookup`
options
(Antoine du Hamel) [https://github.com/nodejs/node/pull/39793]
- doc: deprecate (doc-only) http abort related
(dr-js) [https://github.com/nodejs/node/pull/36670]
- (SEMVER-MAJOR) module: subpath folder mappings EOL
(Guy Bedford) [https://github.com/nodejs/node/pull/40121]
- (SEMVER-MAJOR) module: runtime deprecate trailing slash patterns
(Guy Bedford) [https://github.com/nodejs/node/pull/40117]
OpenSSL 3.0:
Node.js now includes OpenSSL 3.0, specifically https://github.com/quictls/openssl
which provides QUIC support.
While OpenSSL 3.0 APIs should be mostly compatible with those provided
by OpenSSL 1.1.1, we do anticipate some ecosystem impact due to
tightened restrictions on the allowed algorithms and key sizes.
If you hit an `ERR_OSSL_EVP_UNSUPPORTED` error in your application with
Node.js 17, it’s likely that your application or a module you’re using
is attempting to use an algorithm or key size which is no longer allowed
by default with OpenSSL 3.0. A command-line option,
`--openssl-legacy-provider`, has been added to revert to the legacy
provider as a temporary workaround for these tightened restrictions.
For details about all the features in
OpenSSL 3.0 please see https://www.openssl.org/blog/blog/2021/09/07/OpenSSL3.Final.
(Daniel Bevenius) [https://github.com/nodejs/node/pull/38512]
Contributed in https://github.com/nodejs/node/pull/38512, https://github.com/nodejs/node/pull/40478
V8 9.5:
The V8 JavaScript engine is updated to V8 9.5. This release comes with
additional supported types for the `Intl.DisplayNames` API and Extended
`timeZoneName` options in the `Intl.DateTimeFormat` API. You can read
more details in the V8 9.5 release post https://v8.dev/blog/v8-release-95.
(Michaël Zasso) [https://github.com/nodejs/node/pull/40178]
Readline Promise API:
The `readline` module provides an interface for reading data from a
Readable stream (such as `process.stdin`) one line at a time.
(Antoine du Hamel) [https://github.com/nodejs/node/pull/37947]
Other Notable Changes:
- (SEMVER-MAJOR) dns: default to verbatim=true in dns.lookup()
(treysis) [https://github.com/nodejs/node/pull/39987]
- (SEMVER-MAJOR) errors: print Node.js version on fatal exceptions that
cause exit
(Divlo) [https://github.com/nodejs/node/pull/38332]
- deps: upgrade npm to 8.1.0
(npm team) [https://github.com/nodejs/node/pull/40463]
- (SEMVER-MINOR) fs: add FileHandle.prototype.readableWebStream()
(James M Snell) [https://github.com/nodejs/node/pull/39331]
- (SEMVER-MAJOR) lib: add structuredClone() global
(Ethan Arrowood) [https://github.com/nodejs/node/pull/39759]
- (SEMVER-MAJOR) lib: expose `DOMException` as global
(Khaidi Chu) [https://github.com/nodejs/node/pull/39176]
- (SEMVER-MAJOR) stream: finished should error on errored stream
(Robert Nagy) [https://github.com/nodejs/node/pull/39235]
Semver-Major Commits:
- (SEMVER-MAJOR) build: compile with C++17 (MSVC)
(Richard Lau) [https://github.com/nodejs/node/pull/38807]
- (SEMVER-MAJOR) build: compile with --gnu++17
(Richard Lau) [https://github.com/nodejs/node/pull/38807]
- (SEMVER-MAJOR) deps: update V8 to 9.5.172.19
(Michaël Zasso) [https://github.com/nodejs/node/pull/40178]
- (SEMVER-MAJOR) deps,test,src,doc,tools: update to OpenSSL 3.0
(Daniel Bevenius) [https://github.com/nodejs/node/pull/38512]
- (SEMVER-MAJOR) dgram: tighten `address` validation in `socket.send`
(Voltrex) [https://github.com/nodejs/node/pull/39190]
- (SEMVER-MAJOR) dns: runtime deprecate type coercion of `dns.lookup` options
(Antoine du Hamel) [https://github.com/nodejs/node/pull/39793]
- (SEMVER-MAJOR) dns: default to verbatim=true in dns.lookup()
(treysis) [https://github.com/nodejs/node/pull/39987]
- (SEMVER-MAJOR) doc: update minimum supported FreeBSD to 12.2
(Michaël Zasso) [https://github.com/nodejs/node/pull/40179]
- (SEMVER-MAJOR) errors: disp ver on fatal except that causes exit
(Divlo) [https://github.com/nodejs/node/pull/38332]
- (SEMVER-MAJOR) fs: fix rmsync error swallowing
(Nitzan Uziely) [https://github.com/nodejs/node/pull/38684]
- (SEMVER-MAJOR) fs: aggregate errors in fsPromises to avoid error swallowing
(Nitzan Uziely) [https://github.com/nodejs/node/pull/38259]
- (SEMVER-MAJOR) lib: add structuredClone() global
(Ethan Arrowood) [https://github.com/nodejs/node/pull/39759]
- (SEMVER-MAJOR) lib: expose `DOMException` as global
(Khaidi Chu) [https://github.com/nodejs/node/pull/39176]
- (SEMVER-MAJOR) module: subpath folder mappings EOL
(Guy Bedford) [https://github.com/nodejs/node/pull/40121]
- (SEMVER-MAJOR) module: runtime deprecate trailing slash patterns
(Guy Bedford) [https://github.com/nodejs/node/pull/40117]
- (SEMVER-MAJOR) readline: validate `AbortSignal`s and remove unused event listeners
(Antoine du Hamel) [https://github.com/nodejs/node/pull/37947]
- (SEMVER-MAJOR) readline: introduce promise-based API
(Antoine du Hamel) [https://github.com/nodejs/node/pull/37947]
- (SEMVER-MAJOR) readline: refactor `Interface` to ES2015 class
(Antoine du Hamel) [https://github.com/nodejs/node/pull/37947]
- (SEMVER-MAJOR) src: allow CAP\_NET\_BIND\_SERVICE in SafeGetenv
(Daniel Bevenius) [https://github.com/nodejs/node/pull/37727]
- (SEMVER-MAJOR) src: return Maybe from a couple of functions
(Darshan Sen) [https://github.com/nodejs/node/pull/39603]
- (SEMVER-MAJOR) src: allow custom PageAllocator in NodePlatform
(Shelley Vohr) [https://github.com/nodejs/node/pull/38362]
- (SEMVER-MAJOR) stream: fix highwatermark threshold and add the missing error
(Rongjian Zhang) [https://github.com/nodejs/node/pull/38700]
- (SEMVER-MAJOR) stream: don't emit 'data' after 'error' or 'close'
(Robert Nagy) [https://github.com/nodejs/node/pull/39639]
- (SEMVER-MAJOR) stream: do not emit `end` on readable error
(Szymon Marczak) [https://github.com/nodejs/node/pull/39607]
- (SEMVER-MAJOR) stream: forward errored to callback
(Robert Nagy) [https://github.com/nodejs/node/pull/39364]
- (SEMVER-MAJOR) stream: destroy readable on read error
(Robert Nagy) [https://github.com/nodejs/node/pull/39342]
- (SEMVER-MAJOR) stream: validate abort signal
(Robert Nagy) [https://github.com/nodejs/node/pull/39346]
- (SEMVER-MAJOR) stream: unify stream utils
(Robert Nagy) [https://github.com/nodejs/node/pull/39294]
- (SEMVER-MAJOR) stream: throw on premature close in Readable\
(Darshan Sen) [https://github.com/nodejs/node/pull/39117]
- (SEMVER-MAJOR) stream: finished should error on errored stream
(Robert Nagy) [https://github.com/nodejs/node/pull/39235]
- (SEMVER-MAJOR) stream: error Duplex write/read if not writable/readable
(Robert Nagy) [https://github.com/nodejs/node/pull/34385]
- (SEMVER-MAJOR) stream: bypass legacy destroy for pipeline and async iteration
(Robert Nagy) [https://github.com/nodejs/node/pull/38505]
- (SEMVER-MAJOR) url: throw invalid this on detached accessors
(James M Snell) [https://github.com/nodejs/node/pull/39752]
- (SEMVER-MAJOR) url: forbid certain confusable changes from being introduced by toASCII
(Timothy Gu) [https://github.com/nodejs/node/pull/38631]
PR-URL: https://github.com/nodejs/node/pull/40119
2021-09-15 01:55:37 +01:00
|
|
|
- v17.0.0
|
2021-10-18 12:00:42 -04:00
|
|
|
- v16.12.0
|
2021-08-25 22:55:14 +02:00
|
|
|
pr-url: https://github.com/nodejs/node/pull/37468
|
|
|
|
description:
|
2023-08-22 14:53:40 -07:00
|
|
|
Consolidate customization hooks, removed `getFormat`, `getSource`,
|
2021-08-25 22:55:14 +02:00
|
|
|
`transformSource`, and `getGlobalPreloadCode` hooks
|
|
|
|
added `load` and `globalPreload` hooks
|
|
|
|
allowed returning `format` from either `resolve` or `load` hooks.
|
2020-10-23 11:50:44 -07:00
|
|
|
- version:
|
2020-11-22 13:26:13 -08:00
|
|
|
- v15.3.0
|
2021-05-11, Version 14.17.0 'Fermium' (LTS)
Notable Changes:
Diagnostics channel (experimental module):
`diagnostics_channel` is a new experimental module that provides an API
to create named channels to report arbitrary message data for
diagnostics purposes.
The module was initially introduced in Node.js v15.1.0 and is
backported to v14.17.0 to enable testing it at a larger scale.
With `diagnostics_channel`, Node.js core and module authors can publish
contextual data about what they are doing at a given time. This could
be the hostname and query string of a mysql query, for example. Just
create a named channel with `dc.channel(name)` and call
`channel.publish(data)` to send the data to any listeners to that
channel.
```js
const dc = require('diagnostics_channel');
const channel = dc.channel('mysql.query');
MySQL.prototype.query = function query(queryString, values, callback) {
// Broadcast query information whenever a query is made
channel.publish({
query: queryString,
host: this.hostname,
});
this.doQuery(queryString, values, callback);
};
```
Channels are like one big global event emitter but are split into
separate objects to ensure they get the best performance. If nothing is
listening to the channel, the publishing overhead should be as close to
zero as possible. Consuming channel data is as easy as using
`channel.subscribe(listener)` to run a function whenever a message is
published to that channel.
```js
const dc = require('diagnostics_channel');
const channel = dc.channel('mysql.query');
channel.subscribe(({ query, host }) => {
console.log(`mysql query to ${host}: ${query}`);
});
```
The data captured can be used to provide context for what an app is
doing at a given time. This can be used for things like augmenting
tracing data, tracking network and filesystem activity, logging
queries, and many other things. It's also a very useful data source
for diagnostics tools to provide a clearer picture of exactly what the
application is doing at a given point in the data they are presenting.
Contributed by Stephen Belanger (https://github.com/nodejs/node/pull/34895).
UUID support in the crypto module:
The new `crypto.randomUUID()` method now allows to generate random
[RFC 4122](https://www.rfc-editor.org/rfc/rfc4122.txt) Version 4
UUID strings:
```js
const { randomUUID } = require('crypto');
console.log(randomUUID());
// 'aa7c91a1-f8fc-4339-b9db-f93fc7233429'
```
Contributed by James M Snell (https://github.com/nodejs/node/pull/36729).
Experimental support for `AbortController` and `AbortSignal`:
Node.js 14.17.0 adds experimental partial support for `AbortController`
and `AbortSignal`.
Both constructors can be enabled globally using the
`--experimental-abortcontroller` flag.
Additionally, several Node.js APIs have been updated to support
`AbortSignal` for cancellation.
It is not mandatory to use the built-in constructors with them. Any
spec-compliant third-party alternatives should be compatible.
`AbortSignal` support was added to the following methods:
* `child_process.exec`
* `child_process.execFile`
* `child_process.fork`
* `child_process.spawn`
* `dgram.createSocket`
* `events.on`
* `events.once`
* `fs.readFile`
* `fs.watch`
* `fs.writeFile`
* `http.request`
* `https.request`
* `http2Session.request`
* The promisified variants of `setImmediate` and `setTimeout`
Other notable changes:
* doc:
* revoke deprecation of legacy url, change status to legacy (James M Snell) (https://github.com/nodejs/node/pull/37784)
* add legacy status to stability index (James M Snell) (https://github.com/nodejs/node/pull/37784)
* upgrade stability status of report API (Gireesh Punathil) (https://github.com/nodejs/node/pull/35654)
* deps:
* V8: Backport various patches for Apple Silicon support (BoHong Li) (https://github.com/nodejs/node/pull/38051)
* update ICU to 68.1 (Michaël Zasso) (https://github.com/nodejs/node/pull/36187)
* upgrade to libuv 1.41.0 (Colin Ihrig) (https://github.com/nodejs/node/pull/37360)
* http:
* add http.ClientRequest.getRawHeaderNames() (simov) (https://github.com/nodejs/node/pull/37660)
* report request start and end with diagnostics\_channel (Stephen Belanger) (https://github.com/nodejs/node/pull/34895)
* util:
* add getSystemErrorMap() impl (eladkeyshawn) (https://github.com/nodejs/node/pull/38101)
PR-URL: https://github.com/nodejs/node/pull/38507
2021-05-02 23:12:18 -04:00
|
|
|
- v14.17.0
|
2021-03-18 16:52:00 +00:00
|
|
|
- v12.22.0
|
2020-10-23 11:50:44 -07:00
|
|
|
pr-url: https://github.com/nodejs/node/pull/35781
|
|
|
|
description: Stabilize modules implementation.
|
2020-09-28 13:23:53 +02:00
|
|
|
- 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/35249
|
|
|
|
description: Support for detection of CommonJS named exports.
|
|
|
|
- version: v14.8.0
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/34558
|
|
|
|
description: Unflag Top-Level Await.
|
2020-10-22 12:16:39 +02:00
|
|
|
- version:
|
|
|
|
- v14.0.0
|
|
|
|
- v13.14.0
|
2020-11-16 12:04:38 -05:00
|
|
|
- v12.20.0
|
2020-10-22 12:16:39 +02:00
|
|
|
pr-url: https://github.com/nodejs/node/pull/31974
|
|
|
|
description: Remove experimental modules warning.
|
2020-09-28 13:23:53 +02:00
|
|
|
- version:
|
|
|
|
- v13.2.0
|
|
|
|
- v12.17.0
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/29866
|
|
|
|
description: Loading ECMAScript modules no longer requires a command-line flag.
|
|
|
|
- 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.
|
|
|
|
-->
|
2017-06-05 19:44:56 -05:00
|
|
|
|
2020-10-23 11:50:44 -07:00
|
|
|
> Stability: 2 - Stable
|
2017-06-05 19:44:56 -05:00
|
|
|
|
2018-08-28 17:28:46 +02:00
|
|
|
## Introduction
|
|
|
|
|
2017-06-05 19:44:56 -05:00
|
|
|
<!--name=esm-->
|
|
|
|
|
2018-08-28 17:28:46 +02:00
|
|
|
ECMAScript modules are [the official standard format][] to package JavaScript
|
|
|
|
code for reuse. Modules are defined using a variety of [`import`][] and
|
|
|
|
[`export`][] statements.
|
|
|
|
|
2020-04-30 12:32:53 -04:00
|
|
|
The following example of an ES module exports a function:
|
|
|
|
|
|
|
|
```js
|
2020-05-14 15:02:03 -05:00
|
|
|
// addTwo.mjs
|
2020-04-30 12:32:53 -04:00
|
|
|
function addTwo(num) {
|
|
|
|
return num + 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
export { addTwo };
|
|
|
|
```
|
|
|
|
|
2020-05-14 15:02:03 -05:00
|
|
|
The following example of an ES module imports the function from `addTwo.mjs`:
|
2020-04-30 12:32:53 -04:00
|
|
|
|
|
|
|
```js
|
2020-05-14 15:02:03 -05:00
|
|
|
// app.mjs
|
|
|
|
import { addTwo } from './addTwo.mjs';
|
2020-04-30 12:32:53 -04:00
|
|
|
|
|
|
|
// Prints: 6
|
|
|
|
console.log(addTwo(4));
|
|
|
|
```
|
|
|
|
|
2018-08-28 17:28:46 +02:00
|
|
|
Node.js fully supports ECMAScript modules as they are currently specified and
|
2020-10-23 11:50:44 -07:00
|
|
|
provides interoperability between them and its original module format,
|
2018-08-28 17:28:46 +02:00
|
|
|
[CommonJS][].
|
|
|
|
|
2020-08-07 13:04:13 +02:00
|
|
|
<!-- Anchors to make sure old links find a target -->
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
|
|
<i id="esm_package_json_type_field"></i><i id="esm_package_scope_and_file_extensions"></i><i id="esm_input_type_flag"></i>
|
2020-08-07 13:04:13 +02:00
|
|
|
|
2017-06-05 19:44:56 -05:00
|
|
|
## Enabling
|
|
|
|
|
|
|
|
<!-- type=misc -->
|
|
|
|
|
2022-01-17 11:36:19 +01:00
|
|
|
Node.js has two module systems: [CommonJS][] modules and ECMAScript modules.
|
|
|
|
|
2023-10-20 08:44:56 -07:00
|
|
|
Authors can tell Node.js to interpret JavaScript as an ES module via the `.mjs`
|
|
|
|
file extension, the `package.json` [`"type"`][] field with a value `"module"`,
|
2024-12-02 14:43:55 -08:00
|
|
|
or the [`--input-type`][] flag with a value of `"module"`. These are explicit
|
|
|
|
markers of code being intended to run as an ES module.
|
2023-10-20 08:44:56 -07:00
|
|
|
|
2024-12-02 14:43:55 -08:00
|
|
|
Inversely, authors can explicitly tell Node.js to interpret JavaScript as
|
|
|
|
CommonJS via the `.cjs` file extension, the `package.json` [`"type"`][] field
|
|
|
|
with a value `"commonjs"`, or the [`--input-type`][] flag with a value of
|
|
|
|
`"commonjs"`.
|
2023-10-20 08:44:56 -07:00
|
|
|
|
|
|
|
When code lacks explicit markers for either module system, Node.js will inspect
|
|
|
|
the source code of a module to look for ES module syntax. If such syntax is
|
|
|
|
found, Node.js will run the code as an ES module; otherwise it will run the
|
|
|
|
module as CommonJS. See [Determining module system][] for more details.
|
2020-08-07 13:04:13 +02:00
|
|
|
|
|
|
|
<!-- Anchors to make sure old links find a target -->
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
|
|
<i id="esm_package_entry_points"></i><i id="esm_main_entry_point_export"></i><i id="esm_subpath_exports"></i><i id="esm_package_exports_fallbacks"></i><i id="esm_exports_sugar"></i><i id="esm_conditional_exports"></i><i id="esm_nested_conditions"></i><i id="esm_self_referencing_a_package_using_its_name"></i><i id="esm_internal_package_imports"></i><i id="esm_dual_commonjs_es_module_packages"></i><i id="esm_dual_package_hazard"></i><i id="esm_writing_dual_packages_while_avoiding_or_minimizing_hazards"></i><i id="esm_approach_1_use_an_es_module_wrapper"></i><i id="esm_approach_2_isolate_state"></i>
|
2019-04-15 10:29:56 -07:00
|
|
|
|
2019-09-02 18:01:03 -07:00
|
|
|
## Packages
|
|
|
|
|
2020-09-14 17:09:13 +02:00
|
|
|
This section was moved to [Modules: Packages](packages.md).
|
2019-11-09 11:54:13 -08:00
|
|
|
|
2019-12-23 21:32:25 -08:00
|
|
|
## `import` Specifiers
|
2018-08-28 17:28:46 +02:00
|
|
|
|
|
|
|
### Terminology
|
|
|
|
|
|
|
|
The _specifier_ of an `import` statement is the string after the `from` keyword,
|
2022-06-09 05:52:50 -04:00
|
|
|
e.g. `'node:path'` in `import { sep } from 'node:path'`. Specifiers are also
|
|
|
|
used in `export from` statements, and as the argument to an `import()`
|
|
|
|
expression.
|
2018-08-28 17:28:46 +02:00
|
|
|
|
2020-11-08 16:05:55 -08:00
|
|
|
There are three types of specifiers:
|
2018-08-28 17:28:46 +02:00
|
|
|
|
2019-09-13 00:22:29 -04:00
|
|
|
* _Relative specifiers_ like `'./startup.js'` or `'../config.mjs'`. They refer
|
2020-11-08 16:05:55 -08:00
|
|
|
to a path relative to the location of the importing file. _The file extension
|
|
|
|
is always necessary for these._
|
|
|
|
|
|
|
|
* _Bare specifiers_ like `'some-package'` or `'some-package/shuffle'`. They can
|
|
|
|
refer to the main entry point of a package by the package name, or a
|
|
|
|
specific feature module within a package prefixed by the package name as per
|
|
|
|
the examples respectively. _Including the file extension is only necessary
|
|
|
|
for packages without an [`"exports"`][] field._
|
2018-08-28 17:28:46 +02:00
|
|
|
|
2019-09-13 00:22:29 -04:00
|
|
|
* _Absolute specifiers_ like `'file:///opt/nodejs/config.js'`. They refer
|
2018-08-28 17:28:46 +02:00
|
|
|
directly and explicitly to a full path.
|
|
|
|
|
2023-05-19 18:50:50 +03:00
|
|
|
Bare specifier resolutions are handled by the [Node.js module
|
|
|
|
resolution and loading algorithm][].
|
|
|
|
All other specifier resolutions are always only resolved with
|
2020-11-08 16:05:55 -08:00
|
|
|
the standard relative [URL][] resolution semantics.
|
2017-06-05 19:44:56 -05:00
|
|
|
|
2020-11-08 16:05:55 -08:00
|
|
|
Like in CommonJS, module files within packages can be accessed by appending a
|
2022-05-17 21:04:51 +02:00
|
|
|
path to the package name unless the package's [`package.json`][] contains an
|
2020-11-08 16:05:55 -08:00
|
|
|
[`"exports"`][] field, in which case files within packages can only be accessed
|
|
|
|
via the paths defined in [`"exports"`][].
|
2017-06-05 19:44:56 -05:00
|
|
|
|
2020-11-08 16:05:55 -08:00
|
|
|
For details on these package resolution rules that apply to bare specifiers in
|
|
|
|
the Node.js module resolution, see the [packages documentation](packages.md).
|
2017-06-05 19:44:56 -05:00
|
|
|
|
2020-11-08 16:05:55 -08:00
|
|
|
### Mandatory file extensions
|
2020-09-30 15:57:08 -05:00
|
|
|
|
2020-11-08 16:05:55 -08:00
|
|
|
A file extension must be provided when using the `import` keyword to resolve
|
|
|
|
relative or absolute specifiers. Directory indexes (e.g. `'./startup/index.js'`)
|
|
|
|
must also be fully specified.
|
|
|
|
|
|
|
|
This behavior matches how `import` behaves in browser environments, assuming a
|
|
|
|
typically configured server.
|
2020-09-30 15:57:08 -05:00
|
|
|
|
2020-11-08 16:05:55 -08:00
|
|
|
### URLs
|
|
|
|
|
2021-12-07 12:45:04 +01:00
|
|
|
ES modules are resolved and cached as URLs. This means that special characters
|
|
|
|
must be [percent-encoded][], such as `#` with `%23` and `?` with `%3F`.
|
2020-11-08 16:05:55 -08:00
|
|
|
|
|
|
|
`file:`, `node:`, and `data:` URL schemes are supported. A specifier like
|
|
|
|
`'https://example.com/app.js'` is not supported natively in Node.js unless using
|
|
|
|
a [custom HTTPS loader][].
|
|
|
|
|
|
|
|
#### `file:` URLs
|
|
|
|
|
|
|
|
Modules are loaded multiple times if the `import` specifier used to resolve
|
|
|
|
them has a different query or fragment.
|
2020-09-30 15:57:08 -05:00
|
|
|
|
|
|
|
```js
|
2020-11-08 16:05:55 -08:00
|
|
|
import './foo.mjs?query=1'; // loads ./foo.mjs with query of "?query=1"
|
|
|
|
import './foo.mjs?query=2'; // loads ./foo.mjs with query of "?query=2"
|
2020-09-30 15:57:08 -05:00
|
|
|
```
|
|
|
|
|
2022-06-05 17:05:42 +02:00
|
|
|
The volume root may be referenced via `/`, `//`, or `file:///`. Given the
|
2020-11-08 16:05:55 -08:00
|
|
|
differences between [URL][] and path resolution (such as percent encoding
|
|
|
|
details), it is recommended to use [url.pathToFileURL][] when importing a path.
|
|
|
|
|
2022-04-14 11:22:15 -07:00
|
|
|
#### `data:` imports
|
2019-07-09 16:03:07 -05:00
|
|
|
|
|
|
|
<!-- YAML
|
2019-09-04 00:10:04 +02:00
|
|
|
added: v12.10.0
|
2019-07-09 16:03:07 -05:00
|
|
|
-->
|
|
|
|
|
|
|
|
[`data:` URLs][] are supported for importing with the following MIME types:
|
|
|
|
|
2022-04-14 11:22:15 -07:00
|
|
|
* `text/javascript` for ES modules
|
2019-07-09 16:03:07 -05:00
|
|
|
* `application/json` for JSON
|
2020-09-29 05:05:30 -07:00
|
|
|
* `application/wasm` for Wasm
|
2019-07-09 16:03:07 -05:00
|
|
|
|
2019-08-21 12:03:00 +03:00
|
|
|
```js
|
|
|
|
import 'data:text/javascript,console.log("hello!");';
|
2023-10-14 05:52:38 +02:00
|
|
|
import _ from 'data:application/json,"world!"' with { type: 'json' };
|
2019-07-09 16:03:07 -05:00
|
|
|
```
|
|
|
|
|
2022-04-14 11:22:15 -07:00
|
|
|
`data:` URLs only resolve [bare specifiers][Terminology] for builtin modules
|
|
|
|
and [absolute specifiers][Terminology]. Resolving
|
|
|
|
[relative specifiers][Terminology] does not work because `data:` is not a
|
|
|
|
[special scheme][]. For example, attempting to load `./foo`
|
|
|
|
from `data:text/javascript,import "./foo";` fails to resolve because there
|
|
|
|
is no concept of relative resolution for `data:` URLs.
|
|
|
|
|
|
|
|
#### `node:` imports
|
2018-04-15 16:05:55 +03:00
|
|
|
|
2020-11-08 16:05:55 -08:00
|
|
|
<!-- YAML
|
2020-11-16 12:04:38 -05:00
|
|
|
added:
|
|
|
|
- v14.13.1
|
|
|
|
- v12.20.0
|
2021-02-06 12:10:00 +01:00
|
|
|
changes:
|
2021-09-04 15:29:35 +02:00
|
|
|
- version:
|
|
|
|
- v16.0.0
|
|
|
|
- v14.18.0
|
2021-02-06 12:10:00 +01:00
|
|
|
pr-url: https://github.com/nodejs/node/pull/37246
|
|
|
|
description: Added `node:` import support to `require(...)`.
|
2020-11-08 16:05:55 -08:00
|
|
|
-->
|
2017-12-24 16:26:24 +01:00
|
|
|
|
2020-11-08 16:05:55 -08:00
|
|
|
`node:` URLs are supported as an alternative means to load Node.js builtin
|
|
|
|
modules. This URL scheme allows for builtin modules to be referenced by valid
|
|
|
|
absolute URL strings.
|
2018-04-15 16:05:55 +03:00
|
|
|
|
2020-11-08 16:05:55 -08:00
|
|
|
```js
|
|
|
|
import fs from 'node:fs/promises';
|
|
|
|
```
|
2017-12-24 16:26:24 +01:00
|
|
|
|
2023-10-14 05:52:38 +02:00
|
|
|
<a id="import-assertions"></a>
|
|
|
|
|
|
|
|
## Import attributes
|
2021-08-28 01:47:49 +02:00
|
|
|
|
|
|
|
<!-- YAML
|
2022-02-01 00:34:51 -05:00
|
|
|
added:
|
|
|
|
- v17.1.0
|
|
|
|
- v16.14.0
|
2023-10-14 05:52:38 +02:00
|
|
|
changes:
|
2023-11-12 08:52:51 +01:00
|
|
|
- version:
|
|
|
|
- v21.0.0
|
|
|
|
- v20.10.0
|
2024-03-20 17:34:55 +00:00
|
|
|
- v18.20.0
|
2023-10-14 05:52:38 +02:00
|
|
|
pr-url: https://github.com/nodejs/node/pull/50140
|
|
|
|
description: Switch from Import Assertions to Import Attributes.
|
2021-08-28 01:47:49 +02:00
|
|
|
-->
|
|
|
|
|
2024-10-12 13:21:09 +02:00
|
|
|
[Import attributes][Import Attributes MDN] are an inline syntax for module import
|
2021-08-28 01:47:49 +02:00
|
|
|
statements to pass on more information alongside the module specifier.
|
|
|
|
|
|
|
|
```js
|
2023-10-14 05:52:38 +02:00
|
|
|
import fooData from './foo.json' with { type: 'json' };
|
2021-08-28 01:47:49 +02:00
|
|
|
|
|
|
|
const { default: barData } =
|
2023-10-14 05:52:38 +02:00
|
|
|
await import('./bar.json', { with: { type: 'json' } });
|
2021-08-28 01:47:49 +02:00
|
|
|
```
|
|
|
|
|
2024-10-12 13:21:09 +02:00
|
|
|
Node.js only supports the `type` attribute, for which it supports the following values:
|
2021-08-28 01:47:49 +02:00
|
|
|
|
2023-10-14 05:52:38 +02:00
|
|
|
| Attribute `type` | Needed for |
|
2021-11-09 23:23:47 -08:00
|
|
|
| ---------------- | ---------------- |
|
|
|
|
| `'json'` | [JSON modules][] |
|
2021-08-28 01:47:49 +02:00
|
|
|
|
2024-10-12 13:21:09 +02:00
|
|
|
The `type: 'json'` attribute is mandatory when importing JSON modules.
|
|
|
|
|
2024-05-07 09:50:31 +08:00
|
|
|
## Built-in modules
|
2017-06-05 19:44:56 -05:00
|
|
|
|
2024-05-07 09:50:31 +08:00
|
|
|
[Built-in modules][] provide named exports of their public API. A
|
2020-11-08 16:05:55 -08:00
|
|
|
default export is also provided which is the value of the CommonJS exports.
|
|
|
|
The default export can be used for, among other things, modifying the named
|
2024-05-07 09:50:31 +08:00
|
|
|
exports. Named exports of built-in modules are updated only by calling
|
2020-11-08 16:05:55 -08:00
|
|
|
[`module.syncBuiltinESMExports()`][].
|
2018-08-28 17:28:46 +02:00
|
|
|
|
2020-11-08 16:05:55 -08:00
|
|
|
```js
|
2022-04-20 10:23:41 +02:00
|
|
|
import EventEmitter from 'node:events';
|
2020-11-08 16:05:55 -08:00
|
|
|
const e = new EventEmitter();
|
|
|
|
```
|
2017-06-05 19:44:56 -05:00
|
|
|
|
2020-11-08 16:05:55 -08:00
|
|
|
```js
|
2022-04-20 10:23:41 +02:00
|
|
|
import { readFile } from 'node:fs';
|
2020-11-08 16:05:55 -08:00
|
|
|
readFile('./foo.txt', (err, source) => {
|
|
|
|
if (err) {
|
|
|
|
console.error(err);
|
|
|
|
} else {
|
|
|
|
console.log(source);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
```
|
2017-06-05 19:44:56 -05:00
|
|
|
|
2020-11-08 16:05:55 -08:00
|
|
|
```js
|
2022-04-20 10:23:41 +02:00
|
|
|
import fs, { readFileSync } from 'node:fs';
|
|
|
|
import { syncBuiltinESMExports } from 'node:module';
|
|
|
|
import { Buffer } from 'node:buffer';
|
2017-06-05 19:44:56 -05:00
|
|
|
|
2020-11-08 16:05:55 -08:00
|
|
|
fs.readFileSync = () => Buffer.from('Hello, ESM');
|
|
|
|
syncBuiltinESMExports();
|
2017-06-05 19:44:56 -05:00
|
|
|
|
2020-11-08 16:05:55 -08:00
|
|
|
fs.readFileSync === readFileSync;
|
|
|
|
```
|
2018-08-28 17:28:46 +02:00
|
|
|
|
2020-11-08 16:05:55 -08:00
|
|
|
## `import()` expressions
|
2018-08-28 17:28:46 +02:00
|
|
|
|
2020-11-08 16:05:55 -08:00
|
|
|
[Dynamic `import()`][] is supported in both CommonJS and ES modules. In CommonJS
|
|
|
|
modules it can be used to load ES modules.
|
2018-08-28 17:28:46 +02:00
|
|
|
|
2020-11-08 16:05:55 -08:00
|
|
|
## `import.meta`
|
2019-06-19 01:21:23 +02:00
|
|
|
|
2020-11-08 16:05:55 -08:00
|
|
|
* {Object}
|
2019-06-19 01:21:23 +02:00
|
|
|
|
2020-11-08 16:05:55 -08:00
|
|
|
The `import.meta` meta property is an `Object` that contains the following
|
2024-12-11 01:43:13 +00:00
|
|
|
properties. It is only supported in ES modules.
|
2018-08-28 17:28:46 +02:00
|
|
|
|
2023-10-31 17:11:15 -04:00
|
|
|
### `import.meta.dirname`
|
|
|
|
|
|
|
|
<!-- YAML
|
2023-12-12 00:10:33 +00:00
|
|
|
added:
|
|
|
|
- v21.2.0
|
|
|
|
- v20.11.0
|
2025-04-24 09:37:29 -07:00
|
|
|
changes:
|
2025-05-19 12:38:17 +02:00
|
|
|
- version:
|
|
|
|
- v24.0.0
|
|
|
|
- v22.16.0
|
2025-04-24 09:37:29 -07:00
|
|
|
pr-url: https://github.com/nodejs/node/pull/58011
|
|
|
|
description: This property is no longer experimental.
|
2023-10-31 17:11:15 -04:00
|
|
|
-->
|
|
|
|
|
2025-05-01 19:22:50 +02:00
|
|
|
* {string} The directory name of the current module.
|
|
|
|
|
|
|
|
This is the same as the [`path.dirname()`][] of the [`import.meta.filename`][].
|
2023-10-31 17:11:15 -04:00
|
|
|
|
|
|
|
> **Caveat**: only present on `file:` modules.
|
|
|
|
|
|
|
|
### `import.meta.filename`
|
|
|
|
|
|
|
|
<!-- YAML
|
2023-12-12 00:10:33 +00:00
|
|
|
added:
|
|
|
|
- v21.2.0
|
|
|
|
- v20.11.0
|
2025-04-24 09:37:29 -07:00
|
|
|
changes:
|
2025-05-19 12:38:17 +02:00
|
|
|
- version:
|
|
|
|
- v24.0.0
|
|
|
|
- v22.16.0
|
2025-04-24 09:37:29 -07:00
|
|
|
pr-url: https://github.com/nodejs/node/pull/58011
|
|
|
|
description: This property is no longer experimental.
|
2023-10-31 17:11:15 -04:00
|
|
|
-->
|
|
|
|
|
|
|
|
* {string} The full absolute path and filename of the current module, with
|
2024-05-28 01:51:10 +08:00
|
|
|
symlinks resolved.
|
2025-05-01 19:22:50 +02:00
|
|
|
|
|
|
|
This is the same as the [`url.fileURLToPath()`][] of the [`import.meta.url`][].
|
2023-10-31 17:11:15 -04:00
|
|
|
|
|
|
|
> **Caveat** only local modules support this property. Modules not using the
|
|
|
|
> `file:` protocol will not provide it.
|
|
|
|
|
2020-11-08 16:05:55 -08:00
|
|
|
### `import.meta.url`
|
2019-12-19 16:25:52 -05:00
|
|
|
|
2020-11-08 16:05:55 -08:00
|
|
|
* {string} The absolute `file:` URL of the module.
|
2019-12-19 16:25:52 -05:00
|
|
|
|
2020-11-08 16:05:55 -08:00
|
|
|
This is defined exactly the same as it is in browsers providing the URL of the
|
|
|
|
current module file.
|
2019-12-19 16:25:52 -05:00
|
|
|
|
2020-11-08 16:05:55 -08:00
|
|
|
This enables useful patterns such as relative file loading:
|
2019-12-19 16:25:52 -05:00
|
|
|
|
|
|
|
```js
|
2022-04-20 10:23:41 +02:00
|
|
|
import { readFileSync } from 'node:fs';
|
2020-11-08 16:05:55 -08:00
|
|
|
const buffer = readFileSync(new URL('./data.proto', import.meta.url));
|
2019-12-19 16:25:52 -05:00
|
|
|
```
|
|
|
|
|
2025-05-26 23:31:54 +01:00
|
|
|
### `import.meta.main`
|
|
|
|
|
|
|
|
<!-- YAML
|
|
|
|
added:
|
2025-06-08 14:03:03 -04:00
|
|
|
- v24.2.0
|
2025-05-26 23:31:54 +01:00
|
|
|
-->
|
|
|
|
|
|
|
|
> Stability: 1.0 - Early development
|
|
|
|
|
|
|
|
* {boolean} `true` when the current module is the entry point of the current process; `false` otherwise.
|
|
|
|
|
|
|
|
Equivalent to `require.main === module` in CommonJS.
|
|
|
|
|
|
|
|
Analogous to Python's `__name__ == "__main__"`.
|
|
|
|
|
|
|
|
```js
|
|
|
|
export function foo() {
|
|
|
|
return 'Hello, world';
|
|
|
|
}
|
|
|
|
|
|
|
|
function main() {
|
|
|
|
const message = foo();
|
|
|
|
console.log(message);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (import.meta.main) main();
|
|
|
|
// `foo` can be imported from another module without possible side-effects from `main`
|
|
|
|
```
|
|
|
|
|
2023-08-12 22:42:25 -07:00
|
|
|
### `import.meta.resolve(specifier)`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
2023-09-20 14:25:06 +02:00
|
|
|
<!-- YAML
|
2021-05-07 19:31:57 +02:00
|
|
|
added:
|
|
|
|
- v13.9.0
|
|
|
|
- v12.16.2
|
|
|
|
changes:
|
2023-11-27 12:19:49 +01:00
|
|
|
- version:
|
|
|
|
- v20.6.0
|
|
|
|
- v18.19.0
|
2023-09-05 02:23:02 +02:00
|
|
|
pr-url: https://github.com/nodejs/node/pull/49028
|
2024-11-13 05:01:17 +08:00
|
|
|
description: No longer behind `--experimental-import-meta-resolve` CLI flag,
|
|
|
|
except for the non-standard `parentURL` parameter.
|
2023-11-27 12:19:49 +01:00
|
|
|
- version:
|
|
|
|
- v20.6.0
|
|
|
|
- v18.19.0
|
2023-09-20 14:25:06 +02:00
|
|
|
pr-url: https://github.com/nodejs/node/pull/49038
|
|
|
|
description: This API no longer throws when targeting `file:` URLs that do
|
|
|
|
not map to an existing file on the local FS.
|
2023-11-27 12:19:49 +01:00
|
|
|
- version:
|
|
|
|
- v20.0.0
|
|
|
|
- v18.19.0
|
2023-04-13 09:35:17 +02:00
|
|
|
pr-url: https://github.com/nodejs/node/pull/44710
|
|
|
|
description: This API now returns a string synchronously instead of a Promise.
|
2021-09-04 15:29:35 +02:00
|
|
|
- version:
|
|
|
|
- v16.2.0
|
|
|
|
- v14.18.0
|
2021-05-07 19:31:57 +02:00
|
|
|
pr-url: https://github.com/nodejs/node/pull/38587
|
|
|
|
description: Add support for WHATWG `URL` object to `parentURL` parameter.
|
|
|
|
-->
|
2017-06-05 19:44:56 -05:00
|
|
|
|
2023-08-12 22:42:25 -07:00
|
|
|
> Stability: 1.2 - Release candidate
|
2021-04-09 18:28:13 +01:00
|
|
|
|
2023-08-12 22:42:25 -07:00
|
|
|
* `specifier` {string} The module specifier to resolve relative to the
|
|
|
|
current module.
|
2023-09-05 02:23:02 +02:00
|
|
|
* Returns: {string} The absolute URL string that the specifier would resolve to.
|
2017-06-05 19:44:56 -05:00
|
|
|
|
2023-08-12 22:42:25 -07:00
|
|
|
[`import.meta.resolve`][] is a module-relative resolution function scoped to
|
|
|
|
each module, returning the URL string.
|
2021-10-10 21:55:04 -07:00
|
|
|
|
2020-11-08 16:05:55 -08:00
|
|
|
```js
|
2023-04-13 09:35:17 +02:00
|
|
|
const dependencyAsset = import.meta.resolve('component-lib/asset.css');
|
2023-08-12 22:42:25 -07:00
|
|
|
// file:///app/node_modules/component-lib/asset.css
|
2023-09-05 02:23:02 +02:00
|
|
|
import.meta.resolve('./dep.js');
|
|
|
|
// file:///app/dep.js
|
2020-11-08 16:05:55 -08:00
|
|
|
```
|
2017-06-05 19:44:56 -05:00
|
|
|
|
2023-08-12 22:42:25 -07:00
|
|
|
All features of the Node.js module resolution are supported. Dependency
|
|
|
|
resolutions are subject to the permitted exports resolutions within the package.
|
2021-10-10 21:55:04 -07:00
|
|
|
|
2023-09-05 02:23:02 +02:00
|
|
|
**Caveats**:
|
2017-06-05 19:44:56 -05:00
|
|
|
|
2023-09-05 02:23:02 +02:00
|
|
|
* This can result in synchronous file-system operations, which
|
|
|
|
can impact performance similarly to `require.resolve`.
|
|
|
|
* This feature is not available within custom loaders (it would
|
|
|
|
create a deadlock).
|
2023-08-12 22:42:25 -07:00
|
|
|
|
2023-09-05 02:23:02 +02:00
|
|
|
**Non-standard API**:
|
2023-08-12 22:42:25 -07:00
|
|
|
|
2023-09-05 02:23:02 +02:00
|
|
|
When using the `--experimental-import-meta-resolve` flag, that function accepts
|
|
|
|
a second argument:
|
2023-08-12 22:42:25 -07:00
|
|
|
|
2023-09-05 02:23:02 +02:00
|
|
|
* `parent` {string|URL} An optional absolute parent module URL to resolve from.
|
|
|
|
**Default:** `import.meta.url`
|
2023-08-19 18:45:02 +02:00
|
|
|
|
2018-08-28 17:28:46 +02:00
|
|
|
## Interoperability with CommonJS
|
|
|
|
|
2019-12-23 21:32:25 -08:00
|
|
|
### `import` statements
|
2018-08-28 17:28:46 +02:00
|
|
|
|
2020-05-14 22:40:37 -07:00
|
|
|
An `import` statement can reference an ES module or a CommonJS module.
|
2020-11-08 16:05:55 -08:00
|
|
|
`import` statements are permitted only in ES modules, but dynamic [`import()`][]
|
|
|
|
expressions are supported in CommonJS for loading ES modules.
|
2018-08-28 17:28:46 +02:00
|
|
|
|
2021-07-04 20:39:17 -07:00
|
|
|
When importing [CommonJS modules](#commonjs-namespaces), the
|
2020-05-14 22:40:37 -07:00
|
|
|
`module.exports` object is provided as the default export. Named exports may be
|
|
|
|
available, provided by static analysis as a convenience for better ecosystem
|
|
|
|
compatibility.
|
|
|
|
|
2020-11-08 16:05:55 -08:00
|
|
|
### `require`
|
2018-08-28 17:28:46 +02:00
|
|
|
|
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
|
|
|
The CommonJS module `require` currently only supports loading synchronous ES
|
2024-09-26 16:21:37 +02:00
|
|
|
modules (that is, ES modules that do not use top-level `await`).
|
2020-05-14 22:40:37 -07:00
|
|
|
|
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
|
|
|
See [Loading ECMAScript modules using `require()`][] for details.
|
2020-05-14 22:40:37 -07:00
|
|
|
|
2020-11-08 16:05:55 -08:00
|
|
|
### CommonJS Namespaces
|
2020-05-14 22:40:37 -07:00
|
|
|
|
2024-07-14 13:00:49 -07:00
|
|
|
<!-- YAML
|
|
|
|
added: v14.13.0
|
|
|
|
changes:
|
2024-10-09 14:48:01 -03:00
|
|
|
- version: v23.0.0
|
2024-07-14 13:00:49 -07:00
|
|
|
pr-url: https://github.com/nodejs/node/pull/53848
|
|
|
|
description: Added `'module.exports'` export marker to CJS namespaces.
|
|
|
|
-->
|
|
|
|
|
2020-05-14 22:40:37 -07:00
|
|
|
CommonJS modules consist of a `module.exports` object which can be of any type.
|
|
|
|
|
2024-07-14 13:00:49 -07:00
|
|
|
To support this, when importing CommonJS from an ECMAScript module, a namespace
|
|
|
|
wrapper for the CommonJS module is constructed, which always provides a
|
|
|
|
`default` export key pointing to the CommonJS `module.exports` value.
|
|
|
|
|
|
|
|
In addition, a heuristic static analysis is performed against the source text of
|
|
|
|
the CommonJS module to get a best-effort static list of exports to provide on
|
|
|
|
the namespace from values on `module.exports`. This is necessary since these
|
|
|
|
namespaces must be constructed prior to the evaluation of the CJS module.
|
|
|
|
|
|
|
|
These CommonJS namespace objects also provide the `default` export as a
|
|
|
|
`'module.exports'` named export, in order to unambiguously indicate that their
|
|
|
|
representation in CommonJS uses this value, and not the namespace value. This
|
|
|
|
mirrors the semantics of the handling of the `'module.exports'` export name in
|
|
|
|
[`require(esm)`][] interop support.
|
|
|
|
|
2020-05-14 22:40:37 -07:00
|
|
|
When importing a CommonJS module, it can be reliably imported using the ES
|
|
|
|
module default import or its corresponding sugar syntax:
|
2019-11-16 23:35:16 -08:00
|
|
|
|
|
|
|
<!-- eslint-disable no-duplicate-imports -->
|
2021-10-10 21:55:04 -07:00
|
|
|
|
2019-11-16 23:35:16 -08:00
|
|
|
```js
|
2020-05-14 22:40:37 -07:00
|
|
|
import { default as cjs } from 'cjs';
|
2024-11-20 19:10:38 +09:00
|
|
|
// Identical to the above
|
2020-05-14 22:40:37 -07:00
|
|
|
import cjsSugar from 'cjs';
|
|
|
|
|
|
|
|
console.log(cjs);
|
|
|
|
console.log(cjs === cjsSugar);
|
|
|
|
// Prints:
|
|
|
|
// <module.exports>
|
|
|
|
// true
|
2019-11-16 23:35:16 -08:00
|
|
|
```
|
2018-04-22 18:56:42 -05:00
|
|
|
|
2020-05-14 22:40:37 -07:00
|
|
|
This Module Namespace Exotic Object can be directly observed either when using
|
|
|
|
`import * as m from 'cjs'` or a dynamic import:
|
2018-08-28 17:28:46 +02:00
|
|
|
|
2020-05-14 22:40:37 -07:00
|
|
|
<!-- eslint-skip -->
|
2021-10-10 21:55:04 -07:00
|
|
|
|
2020-05-14 22:40:37 -07:00
|
|
|
```js
|
|
|
|
import * as m from 'cjs';
|
|
|
|
console.log(m);
|
|
|
|
console.log(m === await import('cjs'));
|
|
|
|
// Prints:
|
2024-07-14 13:00:49 -07:00
|
|
|
// [Module] { default: <module.exports>, 'module.exports': <module.exports> }
|
2020-05-14 22:40:37 -07:00
|
|
|
// true
|
|
|
|
```
|
2018-08-28 17:28:46 +02:00
|
|
|
|
2020-09-29 05:05:30 -07:00
|
|
|
For better compatibility with existing usage in the JS ecosystem, Node.js
|
|
|
|
in addition attempts to determine the CommonJS named exports of every imported
|
2020-05-14 22:40:37 -07:00
|
|
|
CommonJS module to provide them as separate ES module exports using a static
|
|
|
|
analysis process.
|
2018-08-28 17:28:46 +02:00
|
|
|
|
2020-09-29 05:05:30 -07:00
|
|
|
For example, consider a CommonJS module written:
|
2018-08-28 17:28:46 +02:00
|
|
|
|
2021-06-01 15:10:32 +02:00
|
|
|
```cjs
|
2019-11-15 09:29:18 -08:00
|
|
|
// cjs.cjs
|
2020-05-14 22:40:37 -07:00
|
|
|
exports.name = 'exported';
|
|
|
|
```
|
2018-08-28 17:28:46 +02:00
|
|
|
|
2020-09-29 05:05:30 -07:00
|
|
|
The preceding module supports named imports in ES modules:
|
2018-08-28 17:28:46 +02:00
|
|
|
|
2020-05-14 22:40:37 -07:00
|
|
|
<!-- eslint-disable no-duplicate-imports -->
|
2021-10-10 21:55:04 -07:00
|
|
|
|
2020-05-14 22:40:37 -07:00
|
|
|
```js
|
|
|
|
import { name } from './cjs.cjs';
|
|
|
|
console.log(name);
|
|
|
|
// Prints: 'exported'
|
2018-08-28 17:28:46 +02:00
|
|
|
|
2020-05-14 22:40:37 -07:00
|
|
|
import cjs from './cjs.cjs';
|
|
|
|
console.log(cjs);
|
|
|
|
// Prints: { name: 'exported' }
|
|
|
|
|
|
|
|
import * as m from './cjs.cjs';
|
|
|
|
console.log(m);
|
2024-07-14 13:00:49 -07:00
|
|
|
// Prints:
|
|
|
|
// [Module] {
|
|
|
|
// default: { name: 'exported' },
|
|
|
|
// 'module.exports': { name: 'exported' },
|
|
|
|
// name: 'exported'
|
|
|
|
// }
|
2018-08-28 17:28:46 +02:00
|
|
|
```
|
|
|
|
|
2020-05-14 22:40:37 -07:00
|
|
|
As can be seen from the last example of the Module Namespace Exotic Object being
|
|
|
|
logged, the `name` export is copied off of the `module.exports` object and set
|
|
|
|
directly on the ES module namespace when the module is imported.
|
|
|
|
|
|
|
|
Live binding updates or new exports added to `module.exports` are not detected
|
|
|
|
for these named exports.
|
|
|
|
|
2020-09-29 05:05:30 -07:00
|
|
|
The detection of named exports is based on common syntax patterns but does not
|
|
|
|
always correctly detect named exports. In these cases, using the default
|
2020-05-14 22:40:37 -07:00
|
|
|
import form described above can be a better option.
|
|
|
|
|
|
|
|
Named exports detection covers many common export patterns, reexport patterns
|
|
|
|
and build tool and transpiler outputs. See [cjs-module-lexer][] for the exact
|
|
|
|
semantics implemented.
|
|
|
|
|
2020-11-08 16:05:55 -08:00
|
|
|
### Differences between ES modules and CommonJS
|
2018-08-28 17:28:46 +02:00
|
|
|
|
2022-06-05 17:05:42 +02:00
|
|
|
#### No `require`, `exports`, or `module.exports`
|
2018-04-22 18:56:42 -05:00
|
|
|
|
2020-11-08 16:05:55 -08:00
|
|
|
In most cases, the ES module `import` can be used to load CommonJS modules.
|
|
|
|
|
|
|
|
If needed, a `require` function can be constructed within an ES module using
|
|
|
|
[`module.createRequire()`][].
|
2018-04-22 18:56:42 -05:00
|
|
|
|
2020-11-08 16:05:55 -08:00
|
|
|
#### No `__filename` or `__dirname`
|
|
|
|
|
|
|
|
These CommonJS variables are not available in ES modules.
|
|
|
|
|
|
|
|
`__filename` and `__dirname` use cases can be replicated via
|
2023-10-31 17:11:15 -04:00
|
|
|
[`import.meta.filename`][] and [`import.meta.dirname`][].
|
2020-11-08 16:05:55 -08:00
|
|
|
|
2022-11-29 20:18:28 +09:00
|
|
|
#### No Addon Loading
|
2018-04-22 18:56:42 -05:00
|
|
|
|
2022-11-29 20:18:28 +09:00
|
|
|
[Addons][] are not currently supported with ES module imports.
|
2018-04-22 18:56:42 -05:00
|
|
|
|
2021-02-17 16:09:32 +00:00
|
|
|
They can instead be loaded with [`module.createRequire()`][] or
|
2020-11-08 16:05:55 -08:00
|
|
|
[`process.dlopen`][].
|
2020-05-14 22:40:37 -07:00
|
|
|
|
2025-05-26 23:31:54 +01:00
|
|
|
#### No `require.main`
|
|
|
|
|
|
|
|
To replace `require.main === module`, there is the [`import.meta.main`][] API.
|
|
|
|
|
2020-11-08 16:05:55 -08:00
|
|
|
#### No `require.resolve`
|
2020-05-14 22:40:37 -07:00
|
|
|
|
2020-11-08 16:05:55 -08:00
|
|
|
Relative resolution can be handled via `new URL('./local', import.meta.url)`.
|
2020-05-14 22:40:37 -07:00
|
|
|
|
2023-08-12 22:42:25 -07:00
|
|
|
For a complete `require.resolve` replacement, there is the
|
|
|
|
[import.meta.resolve][] API.
|
2020-05-14 22:40:37 -07:00
|
|
|
|
2020-11-08 16:05:55 -08:00
|
|
|
Alternatively `module.createRequire()` can be used.
|
2020-05-14 22:40:37 -07:00
|
|
|
|
2020-11-08 16:05:55 -08:00
|
|
|
#### No `NODE_PATH`
|
|
|
|
|
|
|
|
`NODE_PATH` is not part of resolving `import` specifiers. Please use symlinks
|
|
|
|
if this behavior is desired.
|
2020-05-14 22:40:37 -07:00
|
|
|
|
2020-11-08 16:05:55 -08:00
|
|
|
#### No `require.extensions`
|
|
|
|
|
2023-08-22 14:53:40 -07:00
|
|
|
`require.extensions` is not used by `import`. Module customization hooks can
|
|
|
|
provide a replacement.
|
2020-11-08 16:05:55 -08:00
|
|
|
|
|
|
|
#### No `require.cache`
|
|
|
|
|
|
|
|
`require.cache` is not used by `import` as the ES module loader has its own
|
|
|
|
separate cache.
|
|
|
|
|
|
|
|
<i id="esm_experimental_json_modules"></i>
|
|
|
|
|
|
|
|
## JSON modules
|
|
|
|
|
2024-11-16 16:36:27 +00:00
|
|
|
<!-- YAML
|
|
|
|
changes:
|
|
|
|
- version:
|
|
|
|
- v23.1.0
|
2024-11-27 16:58:44 -05:00
|
|
|
- v22.12.0
|
2025-01-22 14:04:10 +01:00
|
|
|
- v20.18.3
|
2024-11-16 16:36:27 +00:00
|
|
|
- v18.20.5
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/55333
|
|
|
|
description: JSON modules are no longer experimental.
|
|
|
|
-->
|
|
|
|
|
2022-01-31 14:16:37 -08:00
|
|
|
JSON files can be referenced by `import`:
|
2018-08-28 17:28:46 +02:00
|
|
|
|
|
|
|
```js
|
2023-10-14 05:52:38 +02:00
|
|
|
import packageConfig from './package.json' with { type: 'json' };
|
2018-08-28 17:28:46 +02:00
|
|
|
```
|
|
|
|
|
2023-10-14 05:52:38 +02:00
|
|
|
The `with { type: 'json' }` syntax is mandatory; see [Import Attributes][].
|
2021-11-09 23:23:47 -08:00
|
|
|
|
2022-01-31 14:16:37 -08:00
|
|
|
The imported JSON only exposes a `default` export. There is no support for named
|
|
|
|
exports. A cache entry is created in the CommonJS cache to avoid duplication.
|
|
|
|
The same object is returned in CommonJS if the JSON module has already been
|
|
|
|
imported from the same path.
|
|
|
|
|
2020-11-08 16:05:55 -08:00
|
|
|
<i id="esm_experimental_wasm_modules"></i>
|
|
|
|
|
|
|
|
## Wasm modules
|
|
|
|
|
|
|
|
> Stability: 1 - Experimental
|
2019-04-30 00:27:20 +08:00
|
|
|
|
2025-02-04 11:31:10 -08:00
|
|
|
Importing both WebAssembly module instances and WebAssembly source phase
|
|
|
|
imports are supported under the `--experimental-wasm-modules` flag.
|
2019-04-30 00:27:20 +08:00
|
|
|
|
2025-02-04 11:31:10 -08:00
|
|
|
Both of these integrations are in line with the
|
2021-11-09 20:39:29 -08:00
|
|
|
[ES Module Integration Proposal for WebAssembly][].
|
2019-04-30 00:27:20 +08:00
|
|
|
|
2025-02-04 11:31:10 -08:00
|
|
|
Instance imports allow any `.wasm` files to be imported as normal modules,
|
|
|
|
supporting their module imports in turn.
|
|
|
|
|
|
|
|
For example, an `index.js` containing:
|
2019-04-30 00:27:20 +08:00
|
|
|
|
|
|
|
```js
|
2025-02-04 11:31:10 -08:00
|
|
|
import * as M from './library.wasm';
|
2019-04-30 00:27:20 +08:00
|
|
|
console.log(M);
|
|
|
|
```
|
|
|
|
|
|
|
|
executed under:
|
|
|
|
|
|
|
|
```bash
|
2019-10-11 17:57:13 -04:00
|
|
|
node --experimental-wasm-modules index.mjs
|
2019-04-30 00:27:20 +08:00
|
|
|
```
|
|
|
|
|
2025-02-04 11:31:10 -08:00
|
|
|
would provide the exports interface for the instantiation of `library.wasm`.
|
|
|
|
|
|
|
|
### Wasm Source Phase Imports
|
|
|
|
|
|
|
|
<!-- YAML
|
2025-03-24 16:29:01 -03:00
|
|
|
added: v24.0.0
|
2025-02-04 11:31:10 -08:00
|
|
|
-->
|
|
|
|
|
|
|
|
The [Source Phase Imports][] proposal allows the `import source` keyword
|
|
|
|
combination to import a `WebAssembly.Module` object directly, instead of getting
|
|
|
|
a module instance already instantiated with its dependencies.
|
|
|
|
|
|
|
|
This is useful when needing custom instantiations for Wasm, while still
|
|
|
|
resolving and loading it through the ES module integration.
|
|
|
|
|
|
|
|
For example, to create multiple instances of a module, or to pass custom imports
|
|
|
|
into a new instance of `library.wasm`:
|
|
|
|
|
|
|
|
```js
|
|
|
|
import source libraryModule from './library.wasm';
|
|
|
|
|
2025-05-03 12:26:41 -07:00
|
|
|
const instance1 = await WebAssembly.instantiate(libraryModule, importObject1);
|
2025-02-04 11:31:10 -08:00
|
|
|
|
2025-05-03 12:26:41 -07:00
|
|
|
const instance2 = await WebAssembly.instantiate(libraryModule, importObject2);
|
|
|
|
```
|
|
|
|
|
|
|
|
In addition to the static source phase, there is also a dynamic variant of the
|
|
|
|
source phase via the `import.source` dynamic phase import syntax:
|
|
|
|
|
|
|
|
```js
|
|
|
|
const dynamicLibrary = await import.source('./library.wasm');
|
|
|
|
|
|
|
|
const instance = await WebAssembly.instantiate(dynamicLibrary, importObject);
|
2025-02-04 11:31:10 -08:00
|
|
|
```
|
2019-04-30 00:27:20 +08:00
|
|
|
|
2020-11-08 16:05:55 -08:00
|
|
|
<i id="esm_experimental_top_level_await"></i>
|
|
|
|
|
|
|
|
## Top-level `await`
|
|
|
|
|
2022-01-14 17:56:47 +01:00
|
|
|
<!-- YAML
|
2022-01-10 11:13:35 +01:00
|
|
|
added: v14.8.0
|
|
|
|
-->
|
|
|
|
|
|
|
|
The `await` keyword may be used in the top level body of an ECMAScript module.
|
2019-11-11 22:30:02 -08:00
|
|
|
|
|
|
|
Assuming an `a.mjs` with
|
|
|
|
|
|
|
|
```js
|
|
|
|
export const five = await Promise.resolve(5);
|
|
|
|
```
|
|
|
|
|
|
|
|
And a `b.mjs` with
|
|
|
|
|
|
|
|
```js
|
|
|
|
import { five } from './a.mjs';
|
|
|
|
|
|
|
|
console.log(five); // Logs `5`
|
|
|
|
```
|
|
|
|
|
|
|
|
```bash
|
2020-07-29 18:52:02 -04:00
|
|
|
node b.mjs # works
|
2019-11-11 22:30:02 -08:00
|
|
|
```
|
|
|
|
|
2022-01-10 11:13:35 +01:00
|
|
|
If a top level `await` expression never resolves, the `node` process will exit
|
|
|
|
with a `13` [status code][].
|
|
|
|
|
|
|
|
```js
|
2022-04-20 10:23:41 +02:00
|
|
|
import { spawn } from 'node:child_process';
|
|
|
|
import { execPath } from 'node:process';
|
2022-01-10 11:13:35 +01:00
|
|
|
|
|
|
|
spawn(execPath, [
|
|
|
|
'--input-type=module',
|
|
|
|
'--eval',
|
|
|
|
// Never-resolving Promise:
|
|
|
|
'await new Promise(() => {})',
|
|
|
|
]).once('exit', (code) => {
|
|
|
|
console.log(code); // Logs `13`
|
|
|
|
});
|
|
|
|
```
|
|
|
|
|
2020-11-08 16:05:55 -08:00
|
|
|
<i id="esm_experimental_loaders"></i>
|
|
|
|
|
|
|
|
## Loaders
|
|
|
|
|
2023-08-22 14:53:40 -07:00
|
|
|
The former Loaders documentation is now at
|
|
|
|
[Modules: Customization hooks][Module customization hooks].
|
2023-06-11 21:00:46 -03:00
|
|
|
|
2023-05-19 18:50:50 +03:00
|
|
|
## Resolution and loading algorithm
|
2018-08-28 17:28:46 +02:00
|
|
|
|
|
|
|
### Features
|
|
|
|
|
2023-05-19 18:50:50 +03:00
|
|
|
The default resolver has the following properties:
|
2018-08-28 17:28:46 +02:00
|
|
|
|
|
|
|
* FileURL-based resolution as is used by ES modules
|
|
|
|
* Relative and absolute URL resolution
|
|
|
|
* No default extensions
|
|
|
|
* No folder mains
|
2021-10-10 21:55:04 -07:00
|
|
|
* Bare specifier package resolution lookup through node\_modules
|
2023-05-19 18:50:50 +03:00
|
|
|
* Does not fail on unknown extensions or protocols
|
|
|
|
* Can optionally provide a hint of the format to the loading phase
|
|
|
|
|
|
|
|
The default loader has the following properties
|
2018-08-28 17:28:46 +02:00
|
|
|
|
2023-05-19 18:50:50 +03:00
|
|
|
* Support for builtin module loading via `node:` URLs
|
|
|
|
* Support for "inline" module loading via `data:` URLs
|
|
|
|
* Support for `file:` module loading
|
|
|
|
* Fails on any other URL protocol
|
|
|
|
* Fails on unknown extensions for `file:` loading
|
|
|
|
(supports only `.cjs`, `.js`, and `.mjs`)
|
|
|
|
|
|
|
|
### Resolution algorithm
|
2018-08-28 17:28:46 +02:00
|
|
|
|
|
|
|
The algorithm to load an ES module specifier is given through the
|
2021-10-10 21:55:04 -07:00
|
|
|
**ESM\_RESOLVE** method below. It returns the resolved URL for a
|
2019-12-19 16:25:52 -05:00
|
|
|
module specifier relative to a parentURL.
|
2018-08-28 17:28:46 +02:00
|
|
|
|
2023-05-19 18:50:50 +03:00
|
|
|
The resolution algorithm determines the full resolved URL for a module
|
|
|
|
load, along with its suggested module format. The resolution algorithm
|
|
|
|
does not determine whether the resolved URL protocol can be loaded,
|
|
|
|
or whether the file extensions are permitted, instead these validations
|
|
|
|
are applied by Node.js during the load phase
|
|
|
|
(for example, if it was asked to load a URL that has a protocol that is
|
2024-07-28 13:33:05 -03:00
|
|
|
not `file:`, `data:` or `node:`.
|
2023-05-19 18:50:50 +03:00
|
|
|
|
|
|
|
The algorithm also tries to determine the format of the file based
|
|
|
|
on the extension (see `ESM_FILE_FORMAT` algorithm below). If it does
|
|
|
|
not recognize the file extension (eg if it is not `.mjs`, `.cjs`, or
|
|
|
|
`.json`), then a format of `undefined` is returned,
|
|
|
|
which will throw during the load phase.
|
|
|
|
|
2019-12-19 16:25:52 -05:00
|
|
|
The algorithm to determine the module format of a resolved URL is
|
2023-05-19 18:50:50 +03:00
|
|
|
provided by **ESM\_FILE\_FORMAT**, which returns the unique module
|
2019-12-19 16:25:52 -05:00
|
|
|
format for any file. The _"module"_ format is returned for an ECMAScript
|
|
|
|
Module, while the _"commonjs"_ format is used to indicate loading through the
|
|
|
|
legacy CommonJS loader. Additional formats such as _"addon"_ can be extended in
|
|
|
|
future updates.
|
2018-08-28 17:28:46 +02:00
|
|
|
|
|
|
|
In the following algorithms, all subroutine errors are propagated as errors
|
2019-08-02 01:30:32 -04:00
|
|
|
of these top-level routines unless stated otherwise.
|
2018-08-28 17:28:46 +02:00
|
|
|
|
2020-08-09 16:54:01 -07:00
|
|
|
_defaultConditions_ is the conditional environment name array,
|
2019-12-04 21:14:48 -05:00
|
|
|
`["node", "import"]`.
|
2019-10-13 19:27:39 -04:00
|
|
|
|
2020-02-03 13:31:12 +02:00
|
|
|
The resolver can throw the following errors:
|
2021-10-10 21:55:04 -07:00
|
|
|
|
2020-02-03 13:31:12 +02:00
|
|
|
* _Invalid Module Specifier_: Module specifier is an invalid URL, package name
|
|
|
|
or package subpath specifier.
|
|
|
|
* _Invalid Package Configuration_: package.json configuration is invalid or
|
|
|
|
contains an invalid configuration.
|
2020-06-27 22:09:24 -07:00
|
|
|
* _Invalid Package Target_: Package exports or imports define a target module
|
|
|
|
for the package that is an invalid type or string target.
|
2020-02-03 13:31:12 +02:00
|
|
|
* _Package Path Not Exported_: Package exports do not define or permit a target
|
|
|
|
subpath in the package for the given module.
|
2020-06-27 22:09:24 -07:00
|
|
|
* _Package Import Not Defined_: Package imports do not define the specifier.
|
2020-02-03 13:31:12 +02:00
|
|
|
* _Module Not Found_: The package or module requested does not exist.
|
2021-01-23 13:33:20 +02:00
|
|
|
* _Unsupported Directory Import_: The resolved path corresponds to a directory,
|
|
|
|
which is not a supported target for module imports.
|
2020-02-03 13:31:12 +02:00
|
|
|
|
2023-05-19 18:50:50 +03:00
|
|
|
### Resolution Algorithm Specification
|
2018-08-28 17:28:46 +02:00
|
|
|
|
2021-10-10 21:55:04 -07:00
|
|
|
**ESM\_RESOLVE**(_specifier_, _parentURL_)
|
2019-09-06 01:42:22 -04:00
|
|
|
|
2020-08-09 16:54:01 -07:00
|
|
|
> 1. Let _resolved_ be **undefined**.
|
2021-10-10 21:55:04 -07:00
|
|
|
> 2. If _specifier_ is a valid URL, then
|
2020-08-09 16:54:01 -07:00
|
|
|
> 1. Set _resolved_ to the result of parsing and reserializing
|
2018-08-28 17:28:46 +02:00
|
|
|
> _specifier_ as a URL.
|
2022-06-05 17:05:42 +02:00
|
|
|
> 3. Otherwise, if _specifier_ starts with _"/"_, _"./"_, or _"../"_, then
|
2020-08-09 16:54:01 -07:00
|
|
|
> 1. Set _resolved_ to the URL resolution of _specifier_ relative to
|
2018-08-28 17:28:46 +02:00
|
|
|
> _parentURL_.
|
2021-10-10 21:55:04 -07:00
|
|
|
> 4. Otherwise, if _specifier_ starts with _"#"_, then
|
|
|
|
> 1. Set _resolved_ to the result of
|
|
|
|
> **PACKAGE\_IMPORTS\_RESOLVE**(_specifier_,
|
2021-09-15 02:52:40 -07:00
|
|
|
> _parentURL_, _defaultConditions_).
|
2021-10-10 21:55:04 -07:00
|
|
|
> 5. Otherwise,
|
2018-08-28 17:28:46 +02:00
|
|
|
> 1. Note: _specifier_ is now a bare specifier.
|
2021-10-10 21:55:04 -07:00
|
|
|
> 2. Set _resolved_ the result of
|
|
|
|
> **PACKAGE\_RESOLVE**(_specifier_, _parentURL_).
|
2021-10-18 22:03:07 -07:00
|
|
|
> 6. Let _format_ be **undefined**.
|
|
|
|
> 7. If _resolved_ is a _"file:"_ URL, then
|
|
|
|
> 1. If _resolved_ contains any percent encodings of _"/"_ or _"\\"_ (_"%2F"_
|
|
|
|
> and _"%5C"_ respectively), then
|
|
|
|
> 1. Throw an _Invalid Module Specifier_ error.
|
|
|
|
> 2. If the file at _resolved_ is a directory, then
|
|
|
|
> 1. Throw an _Unsupported Directory Import_ error.
|
|
|
|
> 3. If the file at _resolved_ does not exist, then
|
|
|
|
> 1. Throw a _Module Not Found_ error.
|
|
|
|
> 4. Set _resolved_ to the real path of _resolved_, maintaining the
|
|
|
|
> same URL querystring and fragment components.
|
|
|
|
> 5. Set _format_ to the result of **ESM\_FILE\_FORMAT**(_resolved_).
|
|
|
|
> 8. Otherwise,
|
|
|
|
> 1. Set _format_ the module format of the content type associated with the
|
|
|
|
> URL _resolved_.
|
2023-05-19 18:50:50 +03:00
|
|
|
> 9. Return _format_ and _resolved_ to the loading phase
|
2018-08-28 17:28:46 +02:00
|
|
|
|
2021-10-10 21:55:04 -07:00
|
|
|
**PACKAGE\_RESOLVE**(_packageSpecifier_, _parentURL_)
|
2019-09-06 01:42:22 -04:00
|
|
|
|
2020-08-09 16:54:01 -07:00
|
|
|
> 1. Let _packageName_ be **undefined**.
|
2021-10-10 21:55:04 -07:00
|
|
|
> 2. If _packageSpecifier_ is an empty string, then
|
2020-02-03 13:31:12 +02:00
|
|
|
> 1. Throw an _Invalid Module Specifier_ error.
|
2021-10-18 22:03:07 -07:00
|
|
|
> 3. If _packageSpecifier_ is a Node.js builtin module name, then
|
|
|
|
> 1. Return the string _"node:"_ concatenated with _packageSpecifier_.
|
|
|
|
> 4. If _packageSpecifier_ does not start with _"@"_, then
|
2020-08-09 16:54:01 -07:00
|
|
|
> 1. Set _packageName_ to the substring of _packageSpecifier_ until the first
|
|
|
|
> _"/"_ separator or the end of the string.
|
2021-10-18 22:03:07 -07:00
|
|
|
> 5. Otherwise,
|
2018-08-28 17:28:46 +02:00
|
|
|
> 1. If _packageSpecifier_ does not contain a _"/"_ separator, then
|
2020-02-03 13:31:12 +02:00
|
|
|
> 1. Throw an _Invalid Module Specifier_ error.
|
2021-10-10 21:55:04 -07:00
|
|
|
> 2. Set _packageName_ to the substring of _packageSpecifier_
|
2018-08-28 17:28:46 +02:00
|
|
|
> until the second _"/"_ separator or the end of the string.
|
2021-10-18 22:03:07 -07:00
|
|
|
> 6. If _packageName_ starts with _"."_ or contains _"\\"_ or _"%"_, then
|
2020-02-03 13:31:12 +02:00
|
|
|
> 1. Throw an _Invalid Module Specifier_ error.
|
2021-10-18 22:03:07 -07:00
|
|
|
> 7. Let _packageSubpath_ be _"."_ concatenated with the substring of
|
2021-09-15 02:52:40 -07:00
|
|
|
> _packageSpecifier_ from the position at the length of _packageName_.
|
2025-03-22 11:34:29 -05:00
|
|
|
> 8. Let _selfUrl_ be the result of
|
2021-10-10 21:55:04 -07:00
|
|
|
> **PACKAGE\_SELF\_RESOLVE**(_packageName_, _packageSubpath_, _parentURL_).
|
2025-03-22 11:34:29 -05:00
|
|
|
> 9. If _selfUrl_ is not **undefined**, return _selfUrl_.
|
|
|
|
> 10. While _parentURL_ is not the file system root,
|
2021-10-28 18:27:26 -07:00
|
|
|
> 1. Let _packageURL_ be the URL resolution of _"node\_modules/"_
|
2025-03-22 11:34:29 -05:00
|
|
|
> concatenated with _packageName_, relative to _parentURL_.
|
2021-10-28 18:27:26 -07:00
|
|
|
> 2. Set _parentURL_ to the parent folder URL of _parentURL_.
|
|
|
|
> 3. If the folder at _packageURL_ does not exist, then
|
|
|
|
> 1. Continue the next loop iteration.
|
|
|
|
> 4. Let _pjson_ be the result of **READ\_PACKAGE\_JSON**(_packageURL_).
|
|
|
|
> 5. If _pjson_ is not **null** and _pjson_._exports_ is not **null** or
|
|
|
|
> **undefined**, then
|
|
|
|
> 1. Return the result of **PACKAGE\_EXPORTS\_RESOLVE**(_packageURL_,
|
|
|
|
> _packageSubpath_, _pjson.exports_, _defaultConditions_).
|
|
|
|
> 6. Otherwise, if _packageSubpath_ is equal to _"."_, then
|
|
|
|
> 1. If _pjson.main_ is a string, then
|
|
|
|
> 1. Return the URL resolution of _main_ in _packageURL_.
|
|
|
|
> 7. Otherwise,
|
|
|
|
> 1. Return the URL resolution of _packageSubpath_ in _packageURL_.
|
2025-03-22 11:34:29 -05:00
|
|
|
> 11. Throw a _Module Not Found_ error.
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
|
|
**PACKAGE\_SELF\_RESOLVE**(_packageName_, _packageSubpath_, _parentURL_)
|
|
|
|
|
2021-10-24 15:39:36 -07:00
|
|
|
> 1. Let _packageURL_ be the result of **LOOKUP\_PACKAGE\_SCOPE**(_parentURL_).
|
2021-10-10 21:55:04 -07:00
|
|
|
> 2. If _packageURL_ is **null**, then
|
2019-12-17 01:58:19 -05:00
|
|
|
> 1. Return **undefined**.
|
2021-10-10 21:55:04 -07:00
|
|
|
> 3. Let _pjson_ be the result of **READ\_PACKAGE\_JSON**(_packageURL_).
|
|
|
|
> 4. If _pjson_ is **null** or if _pjson_._exports_ is **null** or
|
2020-08-09 16:54:01 -07:00
|
|
|
> **undefined**, then
|
2019-12-17 01:58:19 -05:00
|
|
|
> 1. Return **undefined**.
|
2021-10-10 21:55:04 -07:00
|
|
|
> 5. If _pjson.name_ is equal to _packageName_, then
|
|
|
|
> 1. Return the result of **PACKAGE\_EXPORTS\_RESOLVE**(_packageURL_,
|
2021-09-30 19:03:42 -07:00
|
|
|
> _packageSubpath_, _pjson.exports_, _defaultConditions_).
|
2021-10-10 21:55:04 -07:00
|
|
|
> 6. Otherwise, return **undefined**.
|
2019-08-19 20:59:25 -07:00
|
|
|
|
2021-10-10 21:55:04 -07:00
|
|
|
**PACKAGE\_EXPORTS\_RESOLVE**(_packageURL_, _subpath_, _exports_, _conditions_)
|
2018-08-28 17:28:46 +02:00
|
|
|
|
2025-03-22 11:34:29 -05:00
|
|
|
Note: This function is directly invoked by the CommonJS resolution algorithm.
|
|
|
|
|
2019-10-13 19:27:39 -04:00
|
|
|
> 1. If _exports_ is an Object with both a key starting with _"."_ and a key not
|
2020-02-03 13:31:12 +02:00
|
|
|
> starting with _"."_, throw an _Invalid Package Configuration_ error.
|
2021-10-10 21:55:04 -07:00
|
|
|
> 2. If _subpath_ is equal to _"."_, then
|
2020-08-09 16:54:01 -07:00
|
|
|
> 1. Let _mainExport_ be **undefined**.
|
2021-10-10 21:55:04 -07:00
|
|
|
> 2. If _exports_ is a String or Array, or an Object containing no keys
|
2020-08-09 16:54:01 -07:00
|
|
|
> starting with _"."_, then
|
|
|
|
> 1. Set _mainExport_ to _exports_.
|
2021-10-10 21:55:04 -07:00
|
|
|
> 3. Otherwise if _exports_ is an Object containing a _"."_ property, then
|
|
|
|
> 1. Set _mainExport_ to _exports_\[_"."_].
|
|
|
|
> 4. If _mainExport_ is not **undefined**, then
|
|
|
|
> 1. Let _resolved_ be the result of **PACKAGE\_TARGET\_RESOLVE**(
|
2022-09-11 03:01:37 -07:00
|
|
|
> _packageURL_, _mainExport_, **null**, **false**, _conditions_).
|
2021-10-10 21:55:04 -07:00
|
|
|
> 2. If _resolved_ is not **null** or **undefined**, return _resolved_.
|
|
|
|
> 3. Otherwise, if _exports_ is an Object and all keys of _exports_ start with
|
2020-08-09 16:54:01 -07:00
|
|
|
> _"."_, then
|
2023-11-29 05:28:44 -05:00
|
|
|
> 1. Assert: _subpath_ begins with _"./"_.
|
2021-10-10 21:55:04 -07:00
|
|
|
> 2. Let _resolved_ be the result of **PACKAGE\_IMPORTS\_EXPORTS\_RESOLVE**(
|
2023-11-29 05:28:44 -05:00
|
|
|
> _subpath_, _exports_, _packageURL_, **false**, _conditions_).
|
2021-10-10 21:55:04 -07:00
|
|
|
> 3. If _resolved_ is not **null** or **undefined**, return _resolved_.
|
|
|
|
> 4. Throw a _Package Path Not Exported_ error.
|
2020-08-09 16:54:01 -07:00
|
|
|
|
2021-10-10 21:55:04 -07:00
|
|
|
**PACKAGE\_IMPORTS\_RESOLVE**(_specifier_, _parentURL_, _conditions_)
|
2020-08-09 16:54:01 -07:00
|
|
|
|
2025-03-22 11:34:29 -05:00
|
|
|
Note: This function is directly invoked by the CommonJS resolution algorithm.
|
|
|
|
|
2020-08-09 16:54:01 -07:00
|
|
|
> 1. Assert: _specifier_ begins with _"#"_.
|
2021-10-10 21:55:04 -07:00
|
|
|
> 2. If _specifier_ is exactly equal to _"#"_ or starts with _"#/"_, then
|
2020-08-09 16:54:01 -07:00
|
|
|
> 1. Throw an _Invalid Module Specifier_ error.
|
2021-10-24 15:39:36 -07:00
|
|
|
> 3. Let _packageURL_ be the result of **LOOKUP\_PACKAGE\_SCOPE**(_parentURL_).
|
2021-10-10 21:55:04 -07:00
|
|
|
> 4. If _packageURL_ is not **null**, then
|
|
|
|
> 1. Let _pjson_ be the result of **READ\_PACKAGE\_JSON**(_packageURL_).
|
|
|
|
> 2. If _pjson.imports_ is a non-null Object, then
|
|
|
|
> 1. Let _resolved_ be the result of
|
|
|
|
> **PACKAGE\_IMPORTS\_EXPORTS\_RESOLVE**(
|
2021-09-15 02:52:40 -07:00
|
|
|
> _specifier_, _pjson.imports_, _packageURL_, **true**, _conditions_).
|
2021-10-10 21:55:04 -07:00
|
|
|
> 2. If _resolved_ is not **null** or **undefined**, return _resolved_.
|
|
|
|
> 5. Throw a _Package Import Not Defined_ error.
|
2020-08-09 16:54:01 -07:00
|
|
|
|
2021-10-10 21:55:04 -07:00
|
|
|
**PACKAGE\_IMPORTS\_EXPORTS\_RESOLVE**(_matchKey_, _matchObj_, _packageURL_,
|
2020-08-09 16:54:01 -07:00
|
|
|
_isImports_, _conditions_)
|
|
|
|
|
2025-03-22 11:34:29 -05:00
|
|
|
> 1. If _matchKey_ ends in _"/"_, then
|
|
|
|
> 1. Throw an _Invalid Module Specifier_ error.
|
|
|
|
> 2. If _matchKey_ is a key of _matchObj_ and does not contain _"\*"_, then
|
2021-10-10 21:55:04 -07:00
|
|
|
> 1. Let _target_ be the value of _matchObj_\[_matchKey_].
|
|
|
|
> 2. Return the result of **PACKAGE\_TARGET\_RESOLVE**(_packageURL_,
|
2022-09-11 03:01:37 -07:00
|
|
|
> _target_, **null**, _isImports_, _conditions_).
|
2025-03-22 11:34:29 -05:00
|
|
|
> 3. Let _expansionKeys_ be the list of keys of _matchObj_ containing only a
|
2021-10-10 21:55:04 -07:00
|
|
|
> single _"\*"_, sorted by the sorting function **PATTERN\_KEY\_COMPARE**
|
|
|
|
> which orders in descending order of specificity.
|
2025-03-22 11:34:29 -05:00
|
|
|
> 4. For each key _expansionKey_ in _expansionKeys_, do
|
2021-09-15 02:52:40 -07:00
|
|
|
> 1. Let _patternBase_ be the substring of _expansionKey_ up to but excluding
|
2021-10-10 21:55:04 -07:00
|
|
|
> the first _"\*"_ character.
|
|
|
|
> 2. If _matchKey_ starts with but is not equal to _patternBase_, then
|
2021-08-02 14:46:23 -07:00
|
|
|
> 1. Let _patternTrailer_ be the substring of _expansionKey_ from the
|
2021-10-10 21:55:04 -07:00
|
|
|
> index after the first _"\*"_ character.
|
|
|
|
> 2. If _patternTrailer_ has zero length, or if _matchKey_ ends with
|
2021-08-02 14:46:23 -07:00
|
|
|
> _patternTrailer_ and the length of _matchKey_ is greater than or
|
|
|
|
> equal to the length of _expansionKey_, then
|
2021-10-10 21:55:04 -07:00
|
|
|
> 1. Let _target_ be the value of _matchObj_\[_expansionKey_].
|
2022-09-11 03:01:37 -07:00
|
|
|
> 2. Let _patternMatch_ be the substring of _matchKey_ starting at the
|
2021-08-02 14:46:23 -07:00
|
|
|
> index of the length of _patternBase_ up to the length of
|
|
|
|
> _matchKey_ minus the length of _patternTrailer_.
|
2021-10-10 21:55:04 -07:00
|
|
|
> 3. Return the result of **PACKAGE\_TARGET\_RESOLVE**(_packageURL_,
|
2022-09-11 03:01:37 -07:00
|
|
|
> _target_, _patternMatch_, _isImports_, _conditions_).
|
2025-03-22 11:34:29 -05:00
|
|
|
> 5. Return **null**.
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
|
|
**PATTERN\_KEY\_COMPARE**(_keyA_, _keyB_)
|
|
|
|
|
2024-06-05 13:20:26 +02:00
|
|
|
> 1. Assert: _keyA_ contains only a single _"\*"_.
|
|
|
|
> 2. Assert: _keyB_ contains only a single _"\*"_.
|
|
|
|
> 3. Let _baseLengthA_ be the index of _"\*"_ in _keyA_.
|
|
|
|
> 4. Let _baseLengthB_ be the index of _"\*"_ in _keyB_.
|
2021-10-10 21:55:04 -07:00
|
|
|
> 5. If _baseLengthA_ is greater than _baseLengthB_, return -1.
|
|
|
|
> 6. If _baseLengthB_ is greater than _baseLengthA_, return 1.
|
2024-06-05 13:20:26 +02:00
|
|
|
> 7. If the length of _keyA_ is greater than the length of _keyB_, return -1.
|
|
|
|
> 8. If the length of _keyB_ is greater than the length of _keyA_, return 1.
|
|
|
|
> 9. Return 0.
|
2021-10-10 21:55:04 -07:00
|
|
|
|
2022-09-11 03:01:37 -07:00
|
|
|
**PACKAGE\_TARGET\_RESOLVE**(_packageURL_, _target_, _patternMatch_,
|
|
|
|
_isImports_, _conditions_)
|
2019-09-06 01:42:22 -04:00
|
|
|
|
2020-03-04 23:53:05 +02:00
|
|
|
> 1. If _target_ is a String, then
|
2022-09-11 03:01:37 -07:00
|
|
|
> 1. If _target_ does not start with _"./"_, then
|
|
|
|
> 1. If _isImports_ is **false**, or if _target_ starts with _"../"_ or
|
|
|
|
> _"/"_, or if _target_ is a valid URL, then
|
|
|
|
> 1. Throw an _Invalid Package Target_ error.
|
|
|
|
> 2. If _patternMatch_ is a String, then
|
|
|
|
> 1. Return **PACKAGE\_RESOLVE**(_target_ with every instance of _"\*"_
|
|
|
|
> replaced by _patternMatch_, _packageURL_ + _"/"_).
|
|
|
|
> 3. Return **PACKAGE\_RESOLVE**(_target_, _packageURL_ + _"/"_).
|
|
|
|
> 2. If _target_ split on _"/"_ or _"\\"_ contains any _""_, _"."_, _".."_,
|
|
|
|
> or _"node\_modules"_ segments after the first _"."_ segment, case
|
|
|
|
> insensitive and including percent encoded variants, throw an _Invalid
|
|
|
|
> Package Target_ error.
|
|
|
|
> 3. Let _resolvedTarget_ be the URL resolution of the concatenation of
|
2019-08-02 01:30:32 -04:00
|
|
|
> _packageURL_ and _target_.
|
2023-11-29 05:28:44 -05:00
|
|
|
> 4. Assert: _packageURL_ is contained in _resolvedTarget_.
|
2022-09-11 03:01:37 -07:00
|
|
|
> 5. If _patternMatch_ is **null**, then
|
|
|
|
> 1. Return _resolvedTarget_.
|
|
|
|
> 6. If _patternMatch_ split on _"/"_ or _"\\"_ contains any _""_, _"."_,
|
|
|
|
> _".."_, or _"node\_modules"_ segments, case insensitive and including
|
|
|
|
> percent encoded variants, throw an _Invalid Module Specifier_ error.
|
|
|
|
> 7. Return the URL resolution of _resolvedTarget_ with every instance of
|
|
|
|
> _"\*"_ replaced with _patternMatch_.
|
2021-10-10 21:55:04 -07:00
|
|
|
> 2. Otherwise, if _target_ is a non-null Object, then
|
2023-11-29 05:28:44 -05:00
|
|
|
> 1. If _target_ contains any index property keys, as defined in ECMA-262
|
2019-12-17 14:00:21 -05:00
|
|
|
> [6.1.7 Array Index][], throw an _Invalid Package Configuration_ error.
|
2021-10-10 21:55:04 -07:00
|
|
|
> 2. For each property _p_ of _target_, in object insertion order as,
|
2020-08-09 16:54:01 -07:00
|
|
|
> 1. If _p_ equals _"default"_ or _conditions_ contains an entry for _p_,
|
|
|
|
> then
|
2019-12-17 14:00:21 -05:00
|
|
|
> 1. Let _targetValue_ be the value of the _p_ property in _target_.
|
2021-10-10 21:55:04 -07:00
|
|
|
> 2. Let _resolved_ be the result of **PACKAGE\_TARGET\_RESOLVE**(
|
2022-09-11 03:01:37 -07:00
|
|
|
> _packageURL_, _targetValue_, _patternMatch_, _isImports_,
|
2020-08-11 18:40:05 -07:00
|
|
|
> _conditions_).
|
2021-10-10 21:55:04 -07:00
|
|
|
> 3. If _resolved_ is equal to **undefined**, continue the loop.
|
|
|
|
> 4. Return _resolved_.
|
|
|
|
> 3. Return **undefined**.
|
|
|
|
> 3. Otherwise, if _target_ is an Array, then
|
|
|
|
> 1. If \_target.length is zero, return **null**.
|
|
|
|
> 2. For each item _targetValue_ in _target_, do
|
|
|
|
> 1. Let _resolved_ be the result of **PACKAGE\_TARGET\_RESOLVE**(
|
2022-09-11 03:01:37 -07:00
|
|
|
> _packageURL_, _targetValue_, _patternMatch_, _isImports_,
|
2020-08-11 18:40:05 -07:00
|
|
|
> _conditions_), continuing the loop on any _Invalid Package Target_
|
|
|
|
> error.
|
2021-10-10 21:55:04 -07:00
|
|
|
> 2. If _resolved_ is **undefined**, continue the loop.
|
|
|
|
> 3. Return _resolved_.
|
|
|
|
> 3. Return or throw the last fallback resolution **null** return or error.
|
|
|
|
> 4. Otherwise, if _target_ is _null_, return **null**.
|
|
|
|
> 5. Otherwise throw an _Invalid Package Target_ error.
|
2018-12-11 18:27:13 +02:00
|
|
|
|
2021-10-18 22:03:07 -07:00
|
|
|
**ESM\_FILE\_FORMAT**(_url_)
|
2019-09-06 01:42:22 -04:00
|
|
|
|
2020-01-19 07:54:27 -08:00
|
|
|
> 1. Assert: _url_ corresponds to an existing file.
|
2021-10-24 15:39:36 -07:00
|
|
|
> 2. If _url_ ends in _".mjs"_, then
|
2018-08-28 17:28:46 +02:00
|
|
|
> 1. Return _"module"_.
|
2021-10-24 15:39:36 -07:00
|
|
|
> 3. If _url_ ends in _".cjs"_, then
|
2018-08-28 17:28:46 +02:00
|
|
|
> 1. Return _"commonjs"_.
|
2021-10-24 15:39:36 -07:00
|
|
|
> 4. If _url_ ends in _".json"_, then
|
2021-10-18 22:03:07 -07:00
|
|
|
> 1. Return _"json"_.
|
2023-10-20 08:44:56 -07:00
|
|
|
> 5. If `--experimental-wasm-modules` is enabled and _url_ ends in
|
|
|
|
> _".wasm"_, then
|
|
|
|
> 1. Return _"wasm"_.
|
2024-12-20 11:59:08 +00:00
|
|
|
> 6. If `--experimental-addon-modules` is enabled and _url_ ends in
|
|
|
|
> _".node"_, then
|
|
|
|
> 1. Return _"addon"_.
|
|
|
|
> 7. Let _packageURL_ be the result of **LOOKUP\_PACKAGE\_SCOPE**(_url_).
|
|
|
|
> 8. Let _pjson_ be the result of **READ\_PACKAGE\_JSON**(_packageURL_).
|
|
|
|
> 9. Let _packageType_ be **null**.
|
|
|
|
> 10. If _pjson?.type_ is _"module"_ or _"commonjs"_, then
|
|
|
|
> 1. Set _packageType_ to _pjson.type_.
|
|
|
|
> 11. If _url_ ends in _".js"_, then
|
2023-10-20 08:44:56 -07:00
|
|
|
> 1. If _packageType_ is not **null**, then
|
|
|
|
> 1. Return _packageType_.
|
2024-07-20 11:30:46 -07:00
|
|
|
> 2. If the result of **DETECT\_MODULE\_SYNTAX**(_source_) is true, then
|
2023-10-20 08:44:56 -07:00
|
|
|
> 1. Return _"module"_.
|
|
|
|
> 3. Return _"commonjs"_.
|
2024-12-20 11:59:08 +00:00
|
|
|
> 12. If _url_ does not have any extension, then
|
2023-10-20 08:44:56 -07:00
|
|
|
> 1. If _packageType_ is _"module"_ and `--experimental-wasm-modules` is
|
|
|
|
> enabled and the file at _url_ contains the header for a WebAssembly
|
|
|
|
> module, then
|
|
|
|
> 1. Return _"wasm"_.
|
|
|
|
> 2. If _packageType_ is not **null**, then
|
|
|
|
> 1. Return _packageType_.
|
2024-07-20 11:30:46 -07:00
|
|
|
> 3. If the result of **DETECT\_MODULE\_SYNTAX**(_source_) is true, then
|
2023-10-20 08:44:56 -07:00
|
|
|
> 1. Return _"module"_.
|
|
|
|
> 4. Return _"commonjs"_.
|
2024-12-20 11:59:08 +00:00
|
|
|
> 13. Return **undefined** (will throw during load phase).
|
2018-08-28 17:28:46 +02:00
|
|
|
|
2021-10-24 15:39:36 -07:00
|
|
|
**LOOKUP\_PACKAGE\_SCOPE**(_url_)
|
2019-09-06 01:42:22 -04:00
|
|
|
|
2018-08-28 17:28:46 +02:00
|
|
|
> 1. Let _scopeURL_ be _url_.
|
2021-10-10 21:55:04 -07:00
|
|
|
> 2. While _scopeURL_ is not the file system root,
|
2020-08-02 17:27:51 -04:00
|
|
|
> 1. Set _scopeURL_ to the parent URL of _scopeURL_.
|
2021-10-10 21:55:04 -07:00
|
|
|
> 2. If _scopeURL_ ends in a _"node\_modules"_ path segment, return **null**.
|
2021-10-24 15:39:36 -07:00
|
|
|
> 3. Let _pjsonURL_ be the resolution of _"package.json"_ within
|
2022-01-18 00:54:10 +08:00
|
|
|
> _scopeURL_.
|
2021-10-24 15:39:36 -07:00
|
|
|
> 4. if the file at _pjsonURL_ exists, then
|
|
|
|
> 1. Return _scopeURL_.
|
2021-10-10 21:55:04 -07:00
|
|
|
> 3. Return **null**.
|
2018-08-28 17:28:46 +02:00
|
|
|
|
2021-10-10 21:55:04 -07:00
|
|
|
**READ\_PACKAGE\_JSON**(_packageURL_)
|
2019-09-06 01:42:22 -04:00
|
|
|
|
2018-08-28 17:28:46 +02:00
|
|
|
> 1. Let _pjsonURL_ be the resolution of _"package.json"_ within _packageURL_.
|
2021-10-10 21:55:04 -07:00
|
|
|
> 2. If the file at _pjsonURL_ does not exist, then
|
2018-08-28 17:28:46 +02:00
|
|
|
> 1. Return **null**.
|
2021-10-10 21:55:04 -07:00
|
|
|
> 3. If the file at _packageURL_ does not parse as valid JSON, then
|
2018-08-28 17:28:46 +02:00
|
|
|
> 1. Throw an _Invalid Package Configuration_ error.
|
2021-10-10 21:55:04 -07:00
|
|
|
> 4. Return the parsed JSON source of the file at _pjsonURL_.
|
2018-08-28 17:28:46 +02:00
|
|
|
|
2024-03-19 20:09:33 -07:00
|
|
|
**DETECT\_MODULE\_SYNTAX**(_source_)
|
|
|
|
|
|
|
|
> 1. Parse _source_ as an ECMAScript module.
|
|
|
|
> 2. If the parse is successful, then
|
|
|
|
> 1. If _source_ contains top-level `await`, static `import` or `export`
|
|
|
|
> statements, or `import.meta`, return **true**.
|
|
|
|
> 2. If _source_ contains a top-level lexical declaration (`const`, `let`,
|
|
|
|
> or `class`) of any of the CommonJS wrapper variables (`require`,
|
|
|
|
> `exports`, `module`, `__filename`, or `__dirname`) then return **true**.
|
|
|
|
> 3. Else return **false**.
|
|
|
|
|
2018-08-28 17:28:46 +02:00
|
|
|
### Customizing ESM specifier resolution algorithm
|
|
|
|
|
2023-08-22 14:53:40 -07:00
|
|
|
[Module customization hooks][] provide a mechanism for customizing the ESM
|
|
|
|
specifier resolution algorithm. An example that provides CommonJS-style
|
|
|
|
resolution for ESM specifiers is [commonjs-extension-resolution-loader][].
|
2018-08-28 17:28:46 +02:00
|
|
|
|
2020-05-14 22:40:37 -07:00
|
|
|
<!-- Note: The cjs-module-lexer link should be kept in-sync with the deps version -->
|
2021-10-10 21:55:04 -07:00
|
|
|
|
2020-09-17 18:53:37 +02:00
|
|
|
[6.1.7 Array Index]: https://tc39.es/ecma262/#integer-index
|
2022-11-29 20:18:28 +09:00
|
|
|
[Addons]: addons.md
|
2024-05-07 09:50:31 +08:00
|
|
|
[Built-in modules]: modules.md#built-in-modules
|
2020-09-14 17:09:13 +02:00
|
|
|
[CommonJS]: modules.md
|
2022-01-17 11:36:19 +01:00
|
|
|
[Determining module system]: packages.md#determining-module-system
|
2022-06-22 16:57:53 +01:00
|
|
|
[Dynamic `import()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/import
|
2021-11-09 20:39:29 -08:00
|
|
|
[ES Module Integration Proposal for WebAssembly]: https://github.com/webassembly/esm-integration
|
2023-10-14 05:52:38 +02:00
|
|
|
[Import Attributes]: #import-attributes
|
2024-10-12 13:21:09 +02:00
|
|
|
[Import Attributes MDN]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import/with
|
2021-08-28 01:47:49 +02:00
|
|
|
[JSON modules]: #json-modules
|
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
|
|
|
[Loading ECMAScript modules using `require()`]: modules.md#loading-ecmascript-modules-using-require
|
2023-08-22 14:53:40 -07:00
|
|
|
[Module customization hooks]: module.md#customization-hooks
|
2023-05-19 18:50:50 +03:00
|
|
|
[Node.js Module Resolution And Loading Algorithm]: #resolution-algorithm-specification
|
2025-02-04 11:31:10 -08:00
|
|
|
[Source Phase Imports]: https://github.com/tc39/proposal-source-phase-imports
|
2021-07-04 20:39:17 -07:00
|
|
|
[Terminology]: #terminology
|
2020-11-08 16:05:55 -08:00
|
|
|
[URL]: https://url.spec.whatwg.org/
|
2021-07-04 20:39:17 -07:00
|
|
|
[`"exports"`]: packages.md#exports
|
|
|
|
[`"type"`]: packages.md#type
|
2022-01-17 11:36:19 +01:00
|
|
|
[`--input-type`]: cli.md#--input-typetype
|
2019-07-09 16:03:07 -05:00
|
|
|
[`data:` URLs]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs
|
2018-08-28 17:28:46 +02:00
|
|
|
[`export`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/export
|
2021-07-04 20:39:17 -07:00
|
|
|
[`import()`]: #import-expressions
|
2023-10-31 17:11:15 -04:00
|
|
|
[`import.meta.dirname`]: #importmetadirname
|
|
|
|
[`import.meta.filename`]: #importmetafilename
|
2025-05-26 23:31:54 +01:00
|
|
|
[`import.meta.main`]: #importmetamain
|
2023-08-12 22:42:25 -07:00
|
|
|
[`import.meta.resolve`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/import.meta/resolve
|
2021-07-04 20:39:17 -07:00
|
|
|
[`import.meta.url`]: #importmetaurl
|
2019-08-21 12:03:00 +03:00
|
|
|
[`import`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import
|
2021-07-04 20:39:17 -07:00
|
|
|
[`module.createRequire()`]: module.md#modulecreaterequirefilename
|
|
|
|
[`module.syncBuiltinESMExports()`]: module.md#modulesyncbuiltinesmexports
|
|
|
|
[`package.json`]: packages.md#nodejs-packagejson-field-definitions
|
2023-10-31 17:11:15 -04:00
|
|
|
[`path.dirname()`]: path.md#pathdirnamepath
|
2021-07-04 20:39:17 -07:00
|
|
|
[`process.dlopen`]: process.md#processdlopenmodule-filename-flags
|
2024-07-14 13:00:49 -07:00
|
|
|
[`require(esm)`]: modules.md#loading-ecmascript-modules-using-require
|
2024-04-24 11:47:48 -04:00
|
|
|
[`url.fileURLToPath()`]: url.md#urlfileurltopathurl-options
|
2025-02-04 08:12:33 -05:00
|
|
|
[cjs-module-lexer]: https://github.com/nodejs/cjs-module-lexer/tree/2.0.0
|
2022-10-04 02:44:08 -07:00
|
|
|
[commonjs-extension-resolution-loader]: https://github.com/nodejs/loaders-test/tree/main/commonjs-extension-resolution-loader
|
2023-08-24 12:27:23 -07:00
|
|
|
[custom https loader]: module.md#import-from-https
|
2023-08-12 22:42:25 -07:00
|
|
|
[import.meta.resolve]: #importmetaresolvespecifier
|
2021-12-07 12:45:04 +01:00
|
|
|
[percent-encoded]: url.md#percent-encoding-in-urls
|
2019-08-21 12:03:00 +03:00
|
|
|
[special scheme]: https://url.spec.whatwg.org/#special-scheme
|
2022-01-10 11:13:35 +01:00
|
|
|
[status code]: process.md#exit-codes
|
2018-08-28 17:28:46 +02:00
|
|
|
[the official standard format]: https://tc39.github.io/ecma262/#sec-modules
|
2024-04-24 11:47:48 -04:00
|
|
|
[url.pathToFileURL]: url.md#urlpathtofileurlpath-options
|