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: nodejs#55241
Reviewed-By: Richard Lau <rlau@redhat.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
  • Loading branch information
joyeecheung authored Oct 7, 2024
1 parent deb5eff commit b0f0252
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 0 deletions.
3 changes: 3 additions & 0 deletions doc/api/modules.md
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,8 @@ experimental and can be disabled using `--no-experimental-require-module`.
When `require()` actually encounters an ES module for the
first time in the process, it will emit an experimental warning. The
warning is expected to be removed when this feature stablizes.
This feature can be detected by checking if
[`process.features.require_module`][] is `true`.

## All together

Expand Down Expand Up @@ -1280,6 +1282,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 @@ -1936,6 +1936,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 @@ -4431,6 +4442,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 @@ -279,6 +279,9 @@ const features = {
get cached_builtins() {
return binding.hasCachedBuiltins();
},
get require_module() {
return getOptionValue('--experimental-require-module');
},
};

ObjectDefineProperty(process, 'features', {
Expand Down
37 changes: 37 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,37 @@
'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, [
'--experimental-require-module',
'-p',
'process.features.require_module',
], {
trim: true,
stdout: 'true',
stderr: '', // Should not emit warnings.
});

// It is now enabled by default.
spawnSyncAndAssert(process.execPath, [
'-p',
'process.features.require_module',
], {
trim: true,
stdout: 'true',
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 b0f0252

Please sign in to comment.