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
Ability to suppress warnings by type (or just experimental warnings) #30810
Comments
Adds a command line option to supress experimental warnings. Currently this cannot be accomplished without supressing all warnings (by using the --no-warnings option). However, once this option is enabled, a user can miss out on essential warnings as this supresses all warnings. This commit adds the --no-experimental-warnings command line option to allow users to ignore warnings they will expect while still being able to monitor unexpected warnings. Fixes: nodejs#30810
|
|
|
@JoshMcCullough Yes the code works, note that the parameters for my replacement function are |
|
I was suggesting that your |
|
https://nodejs.org/dist/latest/docs/api/process.html#process_process_emitwarning_warning_options shows that process.emitWarning can also take an options object as the second argument. I don't think the ESM warning uses that style call but I still check for it as I want my code to continue working if the ESM warning switches to the options object. |
In the absence of something like the feature proposed here:
nodejs/node#30810
Let's just take the heavy-handed approach of suppressing all warnings
unless `--debug` is in effect. Keeps ugly:
(node:19198) ExperimentalWarning: The ESM module loader is experimental.
out of the console. Will rip this all out once it moves out of
experimental status.
Introduce an option `--suppress-warnings` to silence experimental and deprecation warnings for specified features Fixes: nodejs#30810 Co-Authored-By: Cody Deckard <cjdeckard@gmail.com>
|
this would have been cool, especially for those of us using experimental loader (which I presume will be experimental for quite some time?) |
|
The proposed hack with It works when I trigger My cmd arguments are |
|
@ChuanyuanLiu which version of Node are you using? Overriding UPD I found a patch that works both in Node 16 and 18 |
|
A recent change (#42314) converted some of ESMLoader's experimental warning emissions from using |
|
@JakobJingleheimer looks like in Node 18 uses |
|
Could you cite a problematic place where that happens? A quick search of the node codebase suggests it's used only sparingly and in very specific scenarios. emitWarning → doEmitWarning → emit node/lib/internal/process/warning.js Lines 155 to 160 in c3aa86d
but it has done so for ~2 years (according to git history). I'm not aware of a policy change and I see no code docs recommending such a switch (and if it did happen, it was apparently incomplete). It's quite possible one of the ~100 active maintainers used something different recently, either because it was better suited or because they didn't know P.S. Regarding the original topic of this issue, I don't foresee suppressing certain warnings being supported (and it would take a significant amount of work to facilitate as there is currently no authoritative list of feature names, only strings that happen to be consistent because humans care to check). |
And here’s a plain old JavaScript version of it that also silences a few other experimental warnings (JSON modules, and Node.js specifier resolution): const originalEmit = process.emit;
process.emit = function (name, data, ...args) {
if (
name === 'warning' &&
typeof data === 'object' &&
data.name === 'ExperimentalWarning' &&
(data.message.includes('--experimental-loader') ||
data.message.includes('Custom ESM Loaders is an experimental feature') ||
data.message.includes('The Node.js specifier resolution flag is experimental') ||
data.message.includes('Importing JSON modules is an experimental feature'))
)
return false
return originalEmit.apply(process, arguments)
} |
|
I happened to use a smaller version since your code isnt compatible if I use env instead of passing through nodejs' args const originalEmit = process.emit;
process.emit = function (name, data, ...args) {
if (
name === `warning` &&
typeof data === `object` &&
data.name === `ExperimentalWarning`
)
return false;
return originalEmit.apply(process, arguments);
};Heres the bash script that runs mine: NODE_OPTIONS=--experimental-vm-modules node test |
|
(I bet you can add extra conditions to check env, but hey, your code would be not as performant as before then and will be 0.001% slower than before!) |
|
The previous solutions didn't work for me in all cases, though process.removeAllListeners('warning');Alternatively, a safer more fine-grained approach to only disable a specific type of warning ( const warningListeners = process.listeners('warning');
if (warningListeners.length != 1) {
console.warn(
`expected 1 listener on the process "warning" event, saw ${warningListeners.length}`
);
}
if (warningListeners[0]) {
const originalWarningListener = warningListeners[0];
process.removeAllListeners('warning');
process.prependListener('warning', (warning) => {
if (warning.name != 'ExperimentalWarning') {
originalWarningListener(warning);
}
});
}Eventually I'll publish this as |
|
I wrote a package that seems to do it for me atm: suppress-experimental-warnings. CI-tested with many different node versions + operating systems. |
|
Vicious circle here:
Something must yield in order to unstick this deadlock, and this feature request expresses the position that it is the warnings that should yield. |
With localizations too? EDIT: just read your code above and indeed it doesn't seem to rely on output parsing. I'll give it a try. |
This seems to work pretty well FYI... |


coreyfarrell commentedDec 5, 2019
Is your feature request related to a problem? Please describe.
I'd like to suppress experimental warnings while still seeing any other errors. In particular when I am using native ES modules I do not want the experimental warning printed for every process, but I do want unrelated warnings to still be printed.
Describe the solution you'd like
Allow
--no-warningsto optionally accept an option string such as--no-warnings=type1,type2. Using--no-warningswithout any option would continue to disable all warnings. This would allow--no-warnings=ExperimentalWarningto suppress ExperimentalWarning only.Describe alternatives you've considered
--no-experimental-warningsor a similarly named new flag could be created. This has the drawback thatnode --no-experimental-warningson node.js 13.3.0 exit with an error where--no-warnings=ExperimentalWarningswill not currently error (it causes all warnings to be ignored).In my own repo which uses ES modules I've created
suppress-experimental.cjswhich gets loaded withNODE_OPTIONS='--require=./suppress-experimental.cjs':Obviously patching node.js internals like this is undesirable but it accomplishes my goal.
The text was updated successfully, but these errors were encountered: