From f7accdbced578b44d73dfc1d44020fa4d053c219 Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Mon, 7 Oct 2024 17:26:10 +0200 Subject: [PATCH] process: add process.features.require_module For detecting whether `require(esm)` is supported without triggering the experimental warning. PR-URL: https://github.com/nodejs/node/pull/55241 Reviewed-By: Richard Lau Reviewed-By: Matteo Collina --- doc/api/modules.md | 3 ++ doc/api/process.md | 12 ++++++ lib/internal/bootstrap/node.js | 3 ++ .../test-require-module-feature-detect.js | 37 +++++++++++++++++++ test/parallel/test-process-features.js | 1 + 5 files changed, 56 insertions(+) create mode 100644 test/es-module/test-require-module-feature-detect.js diff --git a/doc/api/modules.md b/doc/api/modules.md index 86f5df057e9a2f..9e4d3a223b8df6 100644 --- a/doc/api/modules.md +++ b/doc/api/modules.md @@ -318,6 +318,8 @@ experimental and can be disabled using `--no-experimental-require-module`. When `require()` actually encounters an ES module for the first time in the process, it will emit an experimental warning. The warning is expected to be removed when this feature stablizes. +This feature can be detected by checking if +[`process.features.require_module`][] is `true`. ## All together @@ -1280,6 +1282,7 @@ This section was moved to [`node:test`]: test.md [`package.json`]: packages.md#nodejs-packagejson-field-definitions [`path.dirname()`]: path.md#pathdirnamepath +[`process.features.require_module`]: process.md#processfeaturesrequire_module [`require.main`]: #requiremain [exports shortcut]: #exports-shortcut [module resolution]: #all-together diff --git a/doc/api/process.md b/doc/api/process.md index 0008ecbbc01b40..73d514f1f4a8d2 100644 --- a/doc/api/process.md +++ b/doc/api/process.md @@ -1936,6 +1936,17 @@ added: v0.5.3 A boolean value that is `true` if the current Node.js build includes support for IPv6. +## `process.features.require_module` + + + +* {boolean} + +A boolean value that is `true` if the current Node.js build supports +[loading ECMAScript modules using `require()`][]. + ## `process.features.tls`