From f098dba05531e67d598a5a1c215c520fc65ed755 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 b7f7ee193d2e63..7aa84fe15e8020 100644 --- a/doc/api/modules.md +++ b/doc/api/modules.md @@ -255,6 +255,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 @@ -1218,6 +1220,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 070016a842d565..385ee2f0a751d0 100644 --- a/doc/api/process.md +++ b/doc/api/process.md @@ -1928,6 +1928,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`