Skip to content

Commit f5beef8

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 534c432 commit f5beef8

File tree

4 files changed

+45
-0
lines changed

4 files changed

+45
-0
lines changed

Diff for: doc/api/modules.md

+2
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

Diff for: lib/internal/bootstrap/node.js

+5
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

Diff for: test/es-module/test-require-module-feature-detect.js

+37
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+
});

Diff for: test/parallel/test-process-features.js

+1
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)