Repository navigation
Ability to suppress warnings by type (or just experimental warnings) #30810
Description
Activity
- addedfeature requestIssues requesting new Node.js features.Issues requesting new Node.js features.processIssues and PRs related to the process subsystem.Issues and PRs related to the process subsystem.
on Dec 5, 2019 args[0]would point to a string, since the arguments toemitWarningarewarning: string | Error, name?: string, ctor?: Function. Are you sure this code is working correctly?@JoshMcCullough Yes the code works, note that the parameters for my replacement function are
(warning, ...args). I can see how that looks odd but it definitely works.I was suggesting that your
args[0]will always be a string since it maps to the 2nd parameter ofemitWarning, which is (if the types are correct),name?: string. So the secondifblock would never be entered -- unless I'm totally missing something here.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.
- added a commit that references this issue
on Apr 8, 2020 19 remaining items
Node doesn't localize error messages, they're always in English.
I'm going to close this issue because OP's requested feature exists. You can (for example) disable experimental warnings like so:
$ node --no-warnings=ExperimentalWarning app.jsI'll open a new issue to discuss documentation because this feature seems to be completely undocumented right now.
Reacted by Karl Horky, Bernard, Jacob Smith, paul, Thomas Jan Uta, atul, Conor, Elias Youssef, Wilhelm Uschtrin, drmrbrewer and 20 moreThanks @bnoordhuis, didn't know about this
--no-warningsflag! 🤔 What versions is this available in?I guess this does not allow for disabling warnings of only a specific topic? Eg. disabling only experimental
fetchwarnings - but no other experimental warnings - like in my comment above.Maybe there should be a followup issue created for disabling warnings by their specific topic? I guess that's what the PR #36137 was meant to address.
Reacted by j-hnny, chocolateboy and yon@karlhorky not 100% sure but it's been around for a while so probably all supported release lines.
You're welcome to open a new issue but the previous attempt to implement that functionality stalled out. You'll probably have to drive it yourself if you want to see it happen.
Ok, opened an issue here:
Reacted by chocolateboy and Paul ShryockI want a solution which is simple yet easy to port to any project using fetch. So I shorten it into just 2 lines of code in my entry script.
// Using only 2 line at begining of your entry script(ex: index.js). // Or any place before using fetch to suppress warnings for ExperimentalWarning(just once before calling fetch). // inspired by // https://github.com/nodejs/node/issues/30810#issuecomment-1433950987 const { emit: originalEmit } = process; process.emit = (event, error) => event === 'warning' && error.name === 'ExperimentalWarning' ? false : originalEmit.apply(process, arguments);Reacted by Ren Hiyama, AⱯ and Dmytro Holyshconst { emit: originalEmit } = process; process.emit = (event, error) => event === 'warning' && error.name === 'ExperimentalWarning' ? false : originalEmit.apply(process, arguments);That
argumentsobject isn't from your wrapper function; it's a free variable. The arguments object isn't defined by fat arrow functions. You're accidentally passing originalEmit an unrelated arguments object that Node apparently puts in the prelude for scripts (though not for the REPL), and dropping your actual parameters. Consider using arestparameter instead. See also MDN.Maybe you could share a GitHub repo or NPM package complete with unit and integration tests. It would be nice if your solution also type-checked properly. I'm sure folks would be happy to review your code.
Reacted by AⱯ, Paul Shryock and Abdur-RahmanI add a shebang like this to make the bin script working without showing the warnings. And it works for windows too.
#!/usr/bin/env node --no-warnings=ExperimentalWarningReacted by Bernard, AⱯ, Alexander Smirnoff, Marcolino, Angel Umeh, marslo and an9r0fReacted by Artur KlesunReacted by AⱯ and Angel UmehI add a shebang like this to make the bin script working without showing the warnings. And it works for windows too.
#!/usr/bin/env node --no-warnings=ExperimentalWarningOn my Linux box, this needs to be:
#!/usr/bin/env -S node --no-warnings=ExperimentalWarningThe
-Sflag is needed to split the command line arguments. Without it, the script just hangs, doesn't even start. Does macOSenvsupport the-Sflag?I saw somebody do an executable Dockerfile with
env -Srecently - but does it work out of the box on all platforms today?The other thing you gotta keep in mind when starting a script with a shebang is that it becomes unimportable in
<script type="module">. Which is mostly fine for a CLI entrypoint unless you want to write an entire isomorphic app in 1 file, in which case, no dice 😅Reacted by liudonghua, Albert Mañosa, Qian Li, Joe Chen and networkException@egasimus Yes, I also find this option is necessary when running on linux.
see also liudonghua123/node-sea@04fa6ec.
Reacted by AⱯ@liudonghua123 Yes. Does it still work on Mac and Windows when you add that option?
@liudonghua123 Yes. Does it still work on Mac and Windows when you add that option?
It works on windows and linux, I have not tested on macos yet.
Reacted by AⱯAlso works on macOS! I just tested it.
Reacted by AⱯ and Michael VuoloHi, does anyone know a way of doing this with just environment variables? In my case I have no access to the args of the node binary.
does anyone know a way of doing this with just environment variables?
$ NODE_OPTIONS=--disable-warning=ExperimentalWarning node -e 'require("node:vm").measureMemory()'Reacted by Matt Mundell, Daniel Maricic, Sv443, AⱯ, Maxime Golfier, Yuri Goikhman and Omar AzizCan node process set its own process.env.NODE_OPTIONS ? Or will that not have effect after startup
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.