Skip to content

Commit 76a2fa8

Browse files
committed
process: add process.features.require_module
For detecting whether `require(esm)` is supported without triggering the experimental warning. PR-URL: #55241 Reviewed-By: Richard Lau <[email protected]> Reviewed-By: Matteo Collina <[email protected]>
1 parent af03df3 commit 76a2fa8

File tree

5 files changed

+56
-0
lines changed

5 files changed

+56
-0
lines changed

doc/api/modules.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,8 @@ experimental and can be disabled using `--no-experimental-require-module`.
255255
When `require()` actually encounters an ES module for the
256256
first time in the process, it will emit an experimental warning. The
257257
warning is expected to be removed when this feature stablizes.
258+
This feature can be detected by checking if
259+
[`process.features.require_module`][] is `true`.
258260

259261
## All together
260262

@@ -1218,6 +1220,7 @@ This section was moved to
12181220
[`node:test`]: test.md
12191221
[`package.json`]: packages.md#nodejs-packagejson-field-definitions
12201222
[`path.dirname()`]: path.md#pathdirnamepath
1223+
[`process.features.require_module`]: process.md#processfeaturesrequire_module
12211224
[`require.main`]: #requiremain
12221225
[exports shortcut]: #exports-shortcut
12231226
[module resolution]: #all-together

doc/api/process.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1928,6 +1928,17 @@ added: v0.5.3
19281928
19291929
A boolean value that is `true` if the current Node.js build includes support for IPv6.
19301930
1931+
## `process.features.require_module`
1932+
1933+
<!-- YAML
1934+
added: REPLACEME
1935+
-->
1936+
1937+
* {boolean}
1938+
1939+
A boolean value that is `true` if the current Node.js build supports
1940+
[loading ECMAScript modules using `require()`][].
1941+
19311942
## `process.features.tls`
19321943
19331944
<!-- YAML
@@ -4419,6 +4430,7 @@ cases:
44194430
[built-in modules with mandatory `node:` prefix]: modules.md#built-in-modules-with-mandatory-node-prefix
44204431
[debugger]: debugger.md
44214432
[deprecation code]: deprecations.md
4433+
[loading ECMAScript modules using `require()`]: modules.md#loading-ecmascript-modules-using-require
44224434
[note on process I/O]: #a-note-on-process-io
44234435
[process.cpuUsage]: #processcpuusagepreviousvalue
44244436
[process_emit_warning]: #processemitwarningwarning-type-code-ctor

lib/internal/bootstrap/node.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,9 @@ const features = {
288288
get cached_builtins() {
289289
return binding.hasCachedBuiltins();
290290
},
291+
get require_module() {
292+
return getOptionValue('--experimental-require-module');
293+
},
291294
};
292295

293296
ObjectDefineProperty(process, 'features', {
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
@@ -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)