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 <rlau@redhat.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
This commit is contained in:
Joyee Cheung 2024-10-07 17:26:10 +02:00 committed by GitHub
parent deb5effe01
commit b0f025208f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 56 additions and 0 deletions

View File

@ -318,6 +318,8 @@ experimental and can be disabled using `--no-experimental-require-module`.
When `require()` actually encounters an ES module for the When `require()` actually encounters an ES module for the
first time in the process, it will emit an experimental warning. The first time in the process, it will emit an experimental warning. The
warning is expected to be removed when this feature stablizes. 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 ## All together
@ -1280,6 +1282,7 @@ This section was moved to
[`node:test`]: test.md [`node:test`]: test.md
[`package.json`]: packages.md#nodejs-packagejson-field-definitions [`package.json`]: packages.md#nodejs-packagejson-field-definitions
[`path.dirname()`]: path.md#pathdirnamepath [`path.dirname()`]: path.md#pathdirnamepath
[`process.features.require_module`]: process.md#processfeaturesrequire_module
[`require.main`]: #requiremain [`require.main`]: #requiremain
[exports shortcut]: #exports-shortcut [exports shortcut]: #exports-shortcut
[module resolution]: #all-together [module resolution]: #all-together

View File

@ -1936,6 +1936,17 @@ added: v0.5.3
A boolean value that is `true` if the current Node.js build includes support for IPv6. A boolean value that is `true` if the current Node.js build includes support for IPv6.
## `process.features.require_module`
<!-- YAML
added: REPLACEME
-->
* {boolean}
A boolean value that is `true` if the current Node.js build supports
[loading ECMAScript modules using `require()`][].
## `process.features.tls` ## `process.features.tls`
<!-- YAML <!-- YAML
@ -4431,6 +4442,7 @@ cases:
[built-in modules with mandatory `node:` prefix]: modules.md#built-in-modules-with-mandatory-node-prefix [built-in modules with mandatory `node:` prefix]: modules.md#built-in-modules-with-mandatory-node-prefix
[debugger]: debugger.md [debugger]: debugger.md
[deprecation code]: deprecations.md [deprecation code]: deprecations.md
[loading ECMAScript modules using `require()`]: modules.md#loading-ecmascript-modules-using-require
[note on process I/O]: #a-note-on-process-io [note on process I/O]: #a-note-on-process-io
[process.cpuUsage]: #processcpuusagepreviousvalue [process.cpuUsage]: #processcpuusagepreviousvalue
[process_emit_warning]: #processemitwarningwarning-type-code-ctor [process_emit_warning]: #processemitwarningwarning-type-code-ctor

View File

@ -279,6 +279,9 @@ const features = {
get cached_builtins() { get cached_builtins() {
return binding.hasCachedBuiltins(); return binding.hasCachedBuiltins();
}, },
get require_module() {
return getOptionValue('--experimental-require-module');
},
}; };
ObjectDefineProperty(process, 'features', { ObjectDefineProperty(process, 'features', {

View File

@ -0,0 +1,37 @@
'use strict';
// This tests that process.features.require_module can be used to feature-detect
// require(esm) without triggering a warning.
require('../common');
const { spawnSyncAndAssert } = require('../common/child_process');
spawnSyncAndAssert(process.execPath, [
'--experimental-require-module',
'-p',
'process.features.require_module',
], {
trim: true,
stdout: 'true',
stderr: '', // Should not emit warnings.
});
// It is now enabled by default.
spawnSyncAndAssert(process.execPath, [
'-p',
'process.features.require_module',
], {
trim: true,
stdout: 'true',
stderr: '', // Should not emit warnings.
});
spawnSyncAndAssert(process.execPath, [
'--no-experimental-require-module',
'-p',
'process.features.require_module',
], {
trim: true,
stdout: 'false',
stderr: '', // Should not emit warnings.
});

View File

@ -14,6 +14,7 @@ const expectedKeys = new Map([
['tls_ocsp', ['boolean']], ['tls_ocsp', ['boolean']],
['tls', ['boolean']], ['tls', ['boolean']],
['cached_builtins', ['boolean']], ['cached_builtins', ['boolean']],
['require_module', ['boolean']],
['typescript', ['boolean', 'string']], ['typescript', ['boolean', 'string']],
]); ]);