node/doc/api
Joyee Cheung 0b9249e335
module: implement the "module-sync" exports condition
This patch implements a "module-sync" exports condition
for packages to supply a sycnrhonous ES module to the
Node.js module loader, no matter it's being required
or imported. This is similar to the "module" condition
that bundlers have been using to support `require(esm)`
in Node.js, and allows dual-package authors to opt into
ESM-first only newer versions of Node.js that supports
require(esm) while avoiding the dual-package hazard.

```json
{
  "type": "module",
  "exports": {
    "node": {
      // On new version of Node.js, both require() and import get
      // the ESM version
      "module-sync": "./index.js",
      // On older version of Node.js, where "module" and
      // require(esm) are not supported, use the transpiled CJS version
      // to avoid dual-package hazard. Library authors can decide
      // to drop support for older versions of Node.js when they think
      // it's time.
      "default": "./dist/index.cjs"
    },
    // On any other environment, use the ESM version.
    "default": "./index.js"
  }
}
```

We end up implementing a condition with a different name
instead of reusing "module", because existing code in the
ecosystem using the "module" condition sometimes also expect
the module resolution for these ESM files to work in CJS
style, which is supported by bundlers, but the native
Node.js loader has intentionally made ESM resolution
different from CJS resolution (e.g. forbidding `import
'./noext'` or `import './directory'`), so it would be
semver-major to implement a `"module"` condition
without implementing the forbidden ESM resolution rules.
For now, this just implments a new condition as semver-minor
so it can be backported to older LTS.

Refs: https://webpack.js.org/guides/package-exports/#target-environment-independent-packages
PR-URL: https://github.com/nodejs/node/pull/54648
Fixes: https://github.com/nodejs/node/issues/52173
Refs: https://github.com/joyeecheung/test-module-condition
Refs: https://github.com/nodejs/node/issues/52697
Reviewed-By: Jacob Smith <jacob@frende.me>
Reviewed-By: Jan Krems <jan.krems@gmail.com>
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
2024-09-25 06:35:26 +00:00
..
addons.md src: use args.This() instead of Holder 2024-06-18 16:02:42 +00:00
assert.md doc: require node:process in assert doc examples 2024-07-04 19:49:38 +00:00
async_context.md 2023-04-18, Version 20.0.0 (Current) 2023-04-18 13:04:39 -03:00
async_hooks.md doc,tools: enforce use of node: prefix 2024-07-21 15:44:27 +00:00
buffer.md doc: update buffer.constants.MAX_LENGTH size 2024-08-09 19:19:24 +00:00
child_process.md doc, child_process: add esm snippets 2024-09-05 08:47:48 +00:00
cli.md src: mark node --run as stable 2024-09-21 09:29:47 -07:00
cluster.md doc: move numCPUs require to top of file in cluster CJS example 2024-07-30 06:13:52 +00:00
console.md doc: add esm examples to node:console 2024-08-06 09:56:59 +00:00
corepack.md doc: update pnpm link 2024-03-18 14:27:59 +00:00
crypto.md crypto: add Date fields for validTo and validFrom 2024-09-18 00:56:24 +00:00
debugger.md inspector: introduce the --inspect-wait flag 2024-05-11 18:48:30 +00:00
deprecations.md process: remove process.assert 2024-09-23 16:29:23 +00:00
dgram.md doc: use consistent plural form of "index" 2024-04-07 06:46:29 +00:00
diagnostics_channel.md doc: make some parameters optional in tracingChannel.traceCallback 2024-08-07 15:26:20 +09:30
dns.md doc: add esm examples to node:dns 2024-08-06 18:34:19 +00:00
documentation.md doc: use consistent plural form of "index" 2024-04-07 06:46:29 +00:00
domain.md doc: fix typos 2023-04-23 13:34:08 +00:00
embedding.md test,doc: enable running embedtest for Windows 2024-05-23 18:33:40 +00:00
errors.md test_runner: report error on missing sourcemap source 2024-09-23 00:32:06 +00:00
esm.md lib,src: drop --experimental-network-imports 2024-07-28 16:33:05 +00:00
events.md doc, typings: events.once accepts symbol event type 2024-06-27 21:56:25 +00:00
fs.md doc: clarify that fs.exists() may return false for existing symlink 2024-06-26 09:38:30 +00:00
globals.md doc: add missing EventSource docs to globals 2024-09-22 04:10:15 +00:00
http2.md doc: add esm examples to node:http2 2024-08-11 22:15:08 +00:00
http.md 2024-07-17, Version 22.5.0 (Current) 2024-07-17 12:11:26 -03:00
https.md doc: add esm examples to node:https 2024-08-19 13:52:40 +00:00
index.md module: add --experimental-strip-types 2024-07-24 16:30:06 +00:00
inspector.md 2024-08-22, Version 22.7.0 (Current) 2024-08-22 11:14:41 -03:00
intl.md doc: improve documentation about ICU data fallback 2023-09-22 13:24:23 +00:00
module.md module: implement flushCompileCache() 2024-09-20 19:05:07 +00:00
modules.md module: implement the "module-sync" exports condition 2024-09-25 06:35:26 +00:00
n-api.md 2024-09-17, Version 22.9.0 (Current) 2024-09-17 17:58:54 -03:00
net.md doc: move onread option from socket.connect() to new net.socket() 2024-08-05 23:36:20 +00:00
os.md doc: fix broken Android building link 2024-09-15 12:29:18 +00:00
packages.md module: implement the "module-sync" exports condition 2024-09-25 06:35:26 +00:00
path.md 2024-08-21, Version 20.17.0 'Iron' (LTS) 2024-08-21 18:14:55 +02:00
perf_hooks.md 2024-09-03, Version 22.8.0 (Current) 2024-09-03 10:43:27 -03:00
permissions.md doc: add note on weakness of permission model 2024-08-17 13:03:21 +00:00
process.md doc: fix history of process.features 2024-09-17 14:49:52 +00:00
punycode.md doc: format doc/api/*.md with markdown formatter 2021-10-20 11:59:11 +02:00
querystring.md doc: get rid of unnecessary eslint-skip comments 2023-11-22 19:03:33 +00:00
readline.md doc: detailed description of keystrokes Ctrl-Y and Meta-Y 2023-08-08 19:51:43 +00:00
repl.md doc: remove parseREPLKeyword from REPL documentation 2024-09-22 19:08:53 +00:00
report.md 2024-05-07, Version 20.13.0 'Iron' (LTS) 2024-05-07 17:32:24 +02:00
single-executable-applications.md doc: fix typo in method name in the sea doc 2024-08-15 11:11:01 +00:00
sqlite.md doc: clarify expandedSQL behavior 2024-09-04 19:30:00 +00:00
stream.md 2024-08-21, Version 20.17.0 'Iron' (LTS) 2024-08-21 18:14:55 +02:00
string_decoder.md doc: add print results for examples in StringDecoder 2023-08-27 15:32:47 +00:00
synopsis.md doc: clarify explaination text on how to run the example 2023-10-06 13:37:31 +00:00
test.md doc: cover --experimental-test-module-mocks flag 2024-09-21 23:23:57 +00:00
timers.md timers: document ref option for scheduler.wait 2024-09-02 06:57:02 +00:00
tls.md tls: fix 'ERR_TLS_PSK_SET_IDENTIY_HINT_FAILED' typo 2024-09-20 17:59:22 +00:00
tracing.md doc: include node.module_timer on available categories 2024-07-01 15:18:16 +00:00
tty.md doc,tty: add documentation for ReadStream and WriteStream 2024-07-19 10:53:24 +00:00
typescript.md 2024-08-22, Version 22.7.0 (Current) 2024-08-22 11:14:41 -03:00
url.md url: add missing documentation for URL.parse() 2024-07-05 02:51:03 +00:00
util.md 2024-09-17, Version 22.9.0 (Current) 2024-09-17 17:58:54 -03:00
v8.md doc: add getHeapStatistics() property descriptions 2024-08-29 05:12:56 +00:00
vm.md 2024-09-03, Version 22.8.0 (Current) 2024-09-03 10:43:27 -03:00
wasi.md doc,tools: enforce use of node: prefix 2024-07-21 15:44:27 +00:00
webcrypto.md doc: fix webcrypto.md AES-GCM backticks 2024-08-30 22:32:03 +00:00
webstreams.md 2024-08-21, Version 20.17.0 'Iron' (LTS) 2024-08-21 18:14:55 +02:00
worker_threads.md doc: clarify --max-old-space-size and --max-semi-space-size units 2024-09-06 19:33:03 +02:00
zlib.md 2024-06-20, Version 20.15.0 'Iron' (LTS) 2024-06-20 17:55:41 +02:00