Skip to content

Commit 3e611f0

Browse files
committed
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 12926b6 commit 3e611f0

File tree

4 files changed

+46
-0
lines changed

4 files changed

+46
-0
lines changed

doc/api/modules.md

Lines changed: 3 additions & 0 deletions
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

lib/internal/bootstrap/node.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,9 @@ const features = {
268268
get cached_builtins() {
269269
return binding.hasCachedBuiltins();
270270
},
271+
get require_module() {
272+
return getOptionValue('--experimental-require-module');
273+
},
271274
};
272275

273276
ObjectDefineProperty(process, 'features', {
@@ -299,6 +302,8 @@ ObjectDefineProperty(process, 'features', {
299302
}
300303

301304
const { emitWarning, emitWarningSync } = require('internal/process/warning');
305+
const { getOptionValue } = require('internal/options');
306+
302307
process.emitWarning = emitWarning;
303308
internalBinding('process_methods').setEmitWarningSync(emitWarningSync);
304309

Lines changed: 37 additions & 0 deletions
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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ assert.deepStrictEqual(keys, new Set([
1010
'debug',
1111
'uv',
1212
'ipv6',
13+
'require_module',
1314
'tls_alpn',
1415
'tls_sni',
1516
'tls_ocsp',

0 commit comments

Comments
 (0)