Skip to content

Commit e95163b

Browse files
joyeecheungaduh95
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 <rlau@redhat.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
1 parent 39c7a9e commit e95163b

File tree

5 files changed

+58
-0
lines changed

5 files changed

+58
-0
lines changed

doc/api/modules.md

+4
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,9 @@ If `--experimental-print-required-tla` is enabled, instead of throwing
250250
module, try to locate the top-level awaits, and print their location to
251251
help users fix them.
252252

253+
This feature can be detected by checking if
254+
[`process.features.require_module`][] is `true`.
255+
253256
## All together
254257

255258
<!-- type=misc -->
@@ -1214,6 +1217,7 @@ This section was moved to
12141217
[`node:test`]: test.md
12151218
[`package.json`]: packages.md#nodejs-packagejson-field-definitions
12161219
[`path.dirname()`]: path.md#pathdirnamepath
1220+
[`process.features.require_module`]: process.md#processfeaturesrequire_module
12171221
[`require.main`]: #requiremain
12181222
[exports shortcut]: #exports-shortcut
12191223
[module resolution]: #all-together

doc/api/process.md

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

+3
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', {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+
'--no-warnings',
11+
'--experimental-require-module',
12+
'-p',
13+
'process.features.require_module',
14+
], {
15+
trim: true,
16+
stdout: 'true',
17+
stderr: '', // Should not emit warnings.
18+
});
19+
20+
// It is not enabled by default.
21+
spawnSyncAndAssert(process.execPath, [
22+
'-p',
23+
'process.features.require_module',
24+
], {
25+
trim: true,
26+
stdout: 'false',
27+
stderr: '', // Should not emit warnings.
28+
});
29+
30+
spawnSyncAndAssert(process.execPath, [
31+
'--no-experimental-require-module',
32+
'-p',
33+
'process.features.require_module',
34+
], {
35+
trim: true,
36+
stdout: 'false',
37+
stderr: '', // Should not emit warnings.
38+
});

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)