Skip to content

Ability to suppress warnings by type (or just experimental warnings) #30810

Description

@coreyfarrell

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-warnings to optionally accept an option string such as --no-warnings=type1,type2. Using --no-warnings without any option would continue to disable all warnings. This would allow --no-warnings=ExperimentalWarning to suppress ExperimentalWarning only.

Describe alternatives you've considered
--no-experimental-warnings or a similarly named new flag could be created. This has the drawback that node --no-experimental-warnings on node.js 13.3.0 exit with an error where --no-warnings=ExperimentalWarnings will not currently error (it causes all warnings to be ignored).

In my own repo which uses ES modules I've created suppress-experimental.cjs which gets loaded with NODE_OPTIONS='--require=./suppress-experimental.cjs':

'use strict';

const {emitWarning} = process;

process.emitWarning = (warning, ...args) => {
	if (args[0] === 'ExperimentalWarning') {
		return;
	}

	if (args[0] && typeof args[0] === 'object' && args[0].type === 'ExperimentalWarning') {
		return;
	}

	return emitWarning(warning, ...args);
};

Obviously patching node.js internals like this is undesirable but it accomplishes my goal.

Activity

  1. added
    feature requestIssues requesting new Node.js features.
    processIssues and PRs related to the process subsystem.
    on Dec 5, 2019
  2. JoshMcCullough commented on Mar 6, 2020

    @JoshMcCullough

    args[0] would point to a string, since the arguments to emitWarning are warning: string | Error, name?: string, ctor?: Function. Are you sure this code is working correctly?

  3. coreyfarrell commented on Mar 7, 2020

    @coreyfarrell
    MemberAuthor

    @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.

  4. JoshMcCullough commented on Mar 9, 2020

    @JoshMcCullough

    I was suggesting that your args[0] will always be a string since it maps to the 2nd parameter of emitWarning, which is (if the types are correct), name?: string. So the second if block would never be entered -- unless I'm totally missing something here.

  5. coreyfarrell commented on Mar 9, 2020

    @coreyfarrell
    MemberAuthor

    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.

  6. added a commit that references this issue on Apr 8, 2020
  7. 19 remaining items

  8. bnoordhuis commented on Feb 27, 2023

    @bnoordhuis
    Member

    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.js
    

    I'll open a new issue to discuss documentation because this feature seems to be completely undocumented right now.

  9. karlhorky commented on Feb 27, 2023

    @karlhorky
    Contributor

    Thanks @bnoordhuis, didn't know about this --no-warnings flag! 🤔 What versions is this available in?

    I guess this does not allow for disabling warnings of only a specific topic? Eg. disabling only experimental fetch warnings - 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.

  10. bnoordhuis commented on Feb 27, 2023

    @bnoordhuis
    Member

    @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.

  11. karlhorky commented on Apr 8, 2023

    @karlhorky
    Contributor
  12. Cojad commented on Jul 4, 2023

    @Cojad

    I 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);
    
  13. jeffs commented on Jul 4, 2023

    @jeffs
    const { emit: originalEmit } = process;
    process.emit = (event, error) => event === 'warning' && error.name === 'ExperimentalWarning' ? false : originalEmit.apply(process, arguments);
    

    That arguments object 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 a rest parameter 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.

  14. liudonghua123 commented on Jan 16, 2024

    @liudonghua123
    Contributor

    I 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=ExperimentalWarning
    
  15. egasimus commented on Jan 16, 2024

    @egasimus

    I 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=ExperimentalWarning
    

    On my Linux box, this needs to be:

    #!/usr/bin/env -S node --no-warnings=ExperimentalWarning
    

    The -S flag is needed to split the command line arguments. Without it, the script just hangs, doesn't even start. Does macOS env support the -S flag?

    I saw somebody do an executable Dockerfile with env -S recently - 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 😅

  16. liudonghua123 commented on Jan 16, 2024

    @liudonghua123
    Contributor

    @egasimus Yes, I also find this option is necessary when running on linux.

    see also liudonghua123/node-sea@04fa6ec.

  17. egasimus commented on Jan 16, 2024

    @egasimus

    @liudonghua123 Yes. Does it still work on Mac and Windows when you add that option?

  18. liudonghua123 commented on Jan 16, 2024

    @liudonghua123
    Contributor

    @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.

  19. rauschma commented on Feb 20, 2024

    @rauschma

    Also works on macOS! I just tested it.

  20. Sv443 commented on Jun 1, 2024

    @Sv443

    Hi, 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.

  21. cjihrig commented on Jun 1, 2024

    @cjihrig
    Contributor

    does anyone know a way of doing this with just environment variables?

    $ NODE_OPTIONS=--disable-warning=ExperimentalWarning node -e 'require("node:vm").measureMemory()'
    
  22. crisdosaygo commented on Aug 1, 2024

    @crisdosaygo

    Can node process set its own process.env.NODE_OPTIONS ? Or will that not have effect after startup

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature requestIssues requesting new Node.js features.processIssues and PRs related to the process subsystem.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions