Description
This issue is a followup on #30810 by @coreyfarrell, but specifically only for warnings of a specific type / topic
What is the problem this feature will solve?
After a team has thoughtfully considered using experimental features of their Node.js version (eg. fetch
) and decided the tradeoffs are acceptable for their project, showing the warnings every time their script runs is often not necessary and can be noisy and confusing, making debugging more arduous.
Such a warning, for fetch
in Node.js v18.12.1 (I'm aware that newer versions do not have this warning anymore):
$ node -v
v18.12.1
$ echo "fetch('https://example.com')" > x.mjs
$ node x.mjs
(node:329) ExperimentalWarning: The Fetch API is an experimental feature. This feature could change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
$ node x.mjs
(node:1282) ExperimentalWarning: The Fetch API is an experimental feature. This feature could change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
Replit demo: https://replit.com/@karlhorky/Nodejs-18121-fetch-experimental-warning
What is the feature you are proposing to solve the problem?
It would be great to be able to disable warnings by:
1. Specific warning code(s)
# Disabling a single warning code
node --no-warnings=WARN_FETCH_EXPERIMENTAL
# Disabling multiple warning codes
node --no-warnings=WARN_FETCH_EXPERIMENTAL,WARN_JSON_IMPORT_EXPERIMENTAL
2. Topic(s)
# Disabling a single warning topic
node --no-warnings=WARN_TOPIC_MODULES
# Disabling a single warning topic
node --no-warnings=WARN_TOPIC_EXPERIMENTAL
# Disabling multiple warning topics
node --no-warnings=WARN_TOPIC_MODULES,WARN_TOPIC_FETCH
Prior art / similar APIs
- As mentioned at the top of this issue, these suggestions are similar to what was proposed in Ability to suppress warnings by type (or just experimental warnings) #30810 by @coreyfarrell
- A PR was attempted in this direction: lib: add option to silence warnings #36137
- There is already an (undocumented)
--no-warnings=<cat1>,<cat2>
flag, which does not have the capabilities listed here
What alternatives have you considered?
Workaround
To suppress the experimental fetch
warnings like ExperimentalWarning: The Fetch API is an experimental feature. This feature could change at any time
, this is what we used:
package.json
{
"scripts": {
"start": "node --require ./suppress-experimental-fetch-warnings.js index.js"
}
}
suppress-experimental-fetch-warnings.js
// Suppress Node.js warning about experimental fetch API
// Ref: https://github.com/nodejs/node/issues/30810#issuecomment-1383184769
const originalEmit = process.emit;
process.emit = function (event, error) {
if (
event === 'warning' &&
error.name === 'ExperimentalWarning' &&
error.message.includes('The Fetch API is an experimental feature.')
) {
return false;
}
return originalEmit.apply(process, arguments);
};
An alternative would be to add official Node.js documentation (in a "recipes" or "examples" section) with idiomatic suppression example code + configuration that users could copy into their projects for every warning type.