Skip to content

Commit 4fba019

Browse files
joyeecheungmarco-ippolito
authored andcommitted
process: add process.features.require_module
For detecting whether `require(esm)` is supported without triggering the experimental warning. PR-URL: #55241 Backport-PR-URL: #56927 Reviewed-By: Richard Lau <rlau@redhat.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Refs: #52697
1 parent cf8701c commit 4fba019

File tree

5 files changed

+59
-0
lines changed

5 files changed

+59
-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
@@ -1917,6 +1917,17 @@ added: v0.5.3
19171917
19181918
A boolean value that is `true` if the current Node.js build includes support for IPv6.
19191919
1920+
## `process.features.require_module`
1921+
1922+
<!-- YAML
1923+
added: REPLACEME
1924+
-->
1925+
1926+
* {boolean}
1927+
1928+
A boolean value that is `true` if the current Node.js build supports
1929+
[loading ECMAScript modules using `require()`][].
1930+
19201931
## `process.features.tls`
19211932
19221933
<!-- YAML
@@ -4182,6 +4193,7 @@ cases:
41824193
[built-in modules with mandatory `node:` prefix]: modules.md#built-in-modules-with-mandatory-node-prefix
41834194
[debugger]: debugger.md
41844195
[deprecation code]: deprecations.md
4196+
[loading ECMAScript modules using `require()`]: modules.md#loading-ecmascript-modules-using-require
41854197
[note on process I/O]: #a-note-on-process-io
41864198
[process.cpuUsage]: #processcpuusagepreviousvalue
41874199
[process_emit_warning]: #processemitwarningwarning-type-code-ctor

lib/internal/bootstrap/node.js

+6
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,8 @@ process.assert = deprecate(
251251
'process.assert() is deprecated. Please use the `assert` module instead.',
252252
'DEP0100');
253253

254+
const { getOptionValue } = require('internal/options');
255+
254256
// TODO(joyeecheung): this property has not been well-maintained, should we
255257
// deprecate it in favor of a better API?
256258
const { isDebugBuild, hasOpenSSL, hasInspector } = config;
@@ -268,6 +270,9 @@ const features = {
268270
get cached_builtins() {
269271
return binding.hasCachedBuiltins();
270272
},
273+
get require_module() {
274+
return getOptionValue('--experimental-require-module');
275+
},
271276
};
272277

273278
ObjectDefineProperty(process, 'features', {
@@ -299,6 +304,7 @@ ObjectDefineProperty(process, 'features', {
299304
}
300305

301306
const { emitWarning, emitWarningSync } = require('internal/process/warning');
307+
302308
process.emitWarning = emitWarning;
303309
internalBinding('process_methods').setEmitWarningSync(emitWarningSync);
304310

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
@@ -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)