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 <[email protected]>
Reviewed-By: Matteo Collina <[email protected]>
  • Loading branch information
joyeecheung committed Feb 5, 2025
1 parent 12926b6 commit 3e611f0
Show file tree
Hide file tree
Showing 4 changed files with 46 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
5 changes: 5 additions & 0 deletions lib/internal/bootstrap/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,9 @@ const features = {
get cached_builtins() {
return binding.hasCachedBuiltins();
},
get require_module() {
return getOptionValue('--experimental-require-module');
},
};

ObjectDefineProperty(process, 'features', {
Expand Down Expand Up @@ -299,6 +302,8 @@ ObjectDefineProperty(process, 'features', {
}

const { emitWarning, emitWarningSync } = require('internal/process/warning');
const { getOptionValue } = require('internal/options');

process.emitWarning = emitWarning;
internalBinding('process_methods').setEmitWarningSync(emitWarningSync);

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 @@ -10,6 +10,7 @@ assert.deepStrictEqual(keys, new Set([
'debug',
'uv',
'ipv6',
'require_module',
'tls_alpn',
'tls_sni',
'tls_ocsp',
Expand Down

0 comments on commit 3e611f0

Please sign in to comment.