Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

process: add process.features.require_module #55241

Merged
merged 1 commit into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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`
joyeecheung marked this conversation as resolved.
Show resolved Hide resolved

<!-- 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
Loading