Skip to content

Commit 72e3364

Browse files
joyeecheungtpoisseau
authored andcommitted
process: add process.features.require_module
For detecting whether `require(esm)` is supported without triggering the experimental warning. PR-URL: nodejs#55241 Reviewed-By: Richard Lau <[email protected]> Reviewed-By: Matteo Collina <[email protected]>
1 parent 2a5390e commit 72e3364

File tree

5 files changed

+56
-0
lines changed

5 files changed

+56
-0
lines changed

doc/api/modules.md

+3
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,8 @@ experimental and can be disabled using `--no-experimental-require-module`.
318318
When `require()` actually encounters an ES module for the
319319
first time in the process, it will emit an experimental warning. The
320320
warning is expected to be removed when this feature stablizes.
321+
This feature can be detected by checking if
322+
[`process.features.require_module`][] is `true`.
321323

322324
## All together
323325

@@ -1280,6 +1282,7 @@ This section was moved to
12801282
[`node:test`]: test.md
12811283
[`package.json`]: packages.md#nodejs-packagejson-field-definitions
12821284
[`path.dirname()`]: path.md#pathdirnamepath
1285+
[`process.features.require_module`]: process.md#processfeaturesrequire_module
12831286
[`require.main`]: #requiremain
12841287
[exports shortcut]: #exports-shortcut
12851288
[module resolution]: #all-together

doc/api/process.md

+12
Original file line numberDiff line numberDiff line change
@@ -1936,6 +1936,17 @@ added: v0.5.3
19361936
19371937
A boolean value that is `true` if the current Node.js build includes support for IPv6.
19381938
1939+
## `process.features.require_module`
1940+
1941+
<!-- YAML
1942+
added: REPLACEME
1943+
-->
1944+
1945+
* {boolean}
1946+
1947+
A boolean value that is `true` if the current Node.js build supports
1948+
[loading ECMAScript modules using `require()`][].
1949+
19391950
## `process.features.tls`
19401951
19411952
<!-- YAML
@@ -4431,6 +4442,7 @@ cases:
44314442
[built-in modules with mandatory `node:` prefix]: modules.md#built-in-modules-with-mandatory-node-prefix
44324443
[debugger]: debugger.md
44334444
[deprecation code]: deprecations.md
4445+
[loading ECMAScript modules using `require()`]: modules.md#loading-ecmascript-modules-using-require
44344446
[note on process I/O]: #a-note-on-process-io
44354447
[process.cpuUsage]: #processcpuusagepreviousvalue
44364448
[process_emit_warning]: #processemitwarningwarning-type-code-ctor

lib/internal/bootstrap/node.js

+3
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,9 @@ const features = {
279279
get cached_builtins() {
280280
return binding.hasCachedBuiltins();
281281
},
282+
get require_module() {
283+
return getOptionValue('--experimental-require-module');
284+
},
282285
};
283286

284287
ObjectDefineProperty(process, 'features', {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
'use strict';
2+
3+
// This tests that process.features.require_module can be used to feature-detect
4+
// require(esm) without triggering a warning.
5+
6+
require('../common');
7+
const { spawnSyncAndAssert } = require('../common/child_process');
8+
9+
spawnSyncAndAssert(process.execPath, [
10+
'--experimental-require-module',
11+
'-p',
12+
'process.features.require_module',
13+
], {
14+
trim: true,
15+
stdout: 'true',
16+
stderr: '', // Should not emit warnings.
17+
});
18+
19+
// It is now enabled by default.
20+
spawnSyncAndAssert(process.execPath, [
21+
'-p',
22+
'process.features.require_module',
23+
], {
24+
trim: true,
25+
stdout: 'true',
26+
stderr: '', // Should not emit warnings.
27+
});
28+
29+
spawnSyncAndAssert(process.execPath, [
30+
'--no-experimental-require-module',
31+
'-p',
32+
'process.features.require_module',
33+
], {
34+
trim: true,
35+
stdout: 'false',
36+
stderr: '', // Should not emit warnings.
37+
});

test/parallel/test-process-features.js

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const expectedKeys = new Map([
1414
['tls_ocsp', ['boolean']],
1515
['tls', ['boolean']],
1616
['cached_builtins', ['boolean']],
17+
['require_module', ['boolean']],
1718
['typescript', ['boolean', 'string']],
1819
]);
1920

0 commit comments

Comments
 (0)