Skip to content

Commit

Permalink
process: add process.features.require_module
Browse files Browse the repository at this point in the history
For detecting whether `require(esm)` is supported without triggering
the experimental warning.

PR-URL: #55241
Reviewed-By: Richard Lau <rlau@redhat.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
  • Loading branch information
joyeecheung authored and aduh95 committed Oct 11, 2024
1 parent 39c7a9e commit e95163b
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 0 deletions.
4 changes: 4 additions & 0 deletions doc/api/modules.md
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,9 @@ If `--experimental-print-required-tla` is enabled, instead of throwing
module, try to locate the top-level awaits, and print their location to
help users fix them.

This feature can be detected by checking if
[`process.features.require_module`][] is `true`.

## All together

<!-- type=misc -->
Expand Down Expand Up @@ -1214,6 +1217,7 @@ This section was moved to
[`node:test`]: test.md
[`package.json`]: packages.md#nodejs-packagejson-field-definitions
[`path.dirname()`]: path.md#pathdirnamepath
[`process.features.require_module`]: process.md#processfeaturesrequire_module
[`require.main`]: #requiremain
[exports shortcut]: #exports-shortcut
[module resolution]: #all-together
Expand Down
12 changes: 12 additions & 0 deletions doc/api/process.md
Original file line number Diff line number Diff line change
Expand Up @@ -1928,6 +1928,17 @@ added: v0.5.3
A boolean value that is `true` if the current Node.js build includes support for IPv6.
## `process.features.require_module`
<!-- YAML
added: REPLACEME
-->
* {boolean}
A boolean value that is `true` if the current Node.js build supports
[loading ECMAScript modules using `require()`][].
## `process.features.tls`
<!-- YAML
Expand Down Expand Up @@ -4419,6 +4430,7 @@ cases:
[built-in modules with mandatory `node:` prefix]: modules.md#built-in-modules-with-mandatory-node-prefix
[debugger]: debugger.md
[deprecation code]: deprecations.md
[loading ECMAScript modules using `require()`]: modules.md#loading-ecmascript-modules-using-require
[note on process I/O]: #a-note-on-process-io
[process.cpuUsage]: #processcpuusagepreviousvalue
[process_emit_warning]: #processemitwarningwarning-type-code-ctor
Expand Down
3 changes: 3 additions & 0 deletions lib/internal/bootstrap/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,9 @@ const features = {
get cached_builtins() {
return binding.hasCachedBuiltins();
},
get require_module() {
return getOptionValue('--experimental-require-module');
},
};

ObjectDefineProperty(process, 'features', {
Expand Down
38 changes: 38 additions & 0 deletions test/es-module/test-require-module-feature-detect.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
'use strict';

// This tests that process.features.require_module can be used to feature-detect
// require(esm) without triggering a warning.

require('../common');
const { spawnSyncAndAssert } = require('../common/child_process');

spawnSyncAndAssert(process.execPath, [
'--no-warnings',
'--experimental-require-module',
'-p',
'process.features.require_module',
], {
trim: true,
stdout: 'true',
stderr: '', // Should not emit warnings.
});

// It is not enabled by default.
spawnSyncAndAssert(process.execPath, [
'-p',
'process.features.require_module',
], {
trim: true,
stdout: 'false',
stderr: '', // Should not emit warnings.
});

spawnSyncAndAssert(process.execPath, [
'--no-experimental-require-module',
'-p',
'process.features.require_module',
], {
trim: true,
stdout: 'false',
stderr: '', // Should not emit warnings.
});
1 change: 1 addition & 0 deletions test/parallel/test-process-features.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const expectedKeys = new Map([
['tls_ocsp', ['boolean']],
['tls', ['boolean']],
['cached_builtins', ['boolean']],
['require_module', ['boolean']],
['typescript', ['boolean', 'string']],
]);

Expand Down

0 comments on commit e95163b

Please sign in to comment.