镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

Ability to suppress warnings by type #47478

Description

@karlhorky

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

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.

Activity

  1. SimenB commented on Apr 8, 2023

    @SimenB
    Member

    +1 to this. Since the vm ESM APIs remain experimental, it'd be great to suppress its warnings when running Jest with native ESM.

    https://jestjs.io/docs/ecmascript-modules

  2. axkibe commented on Apr 10, 2023

    @axkibe
    Contributor

    Honestly if the command line explicitly turns on an experimental feature I don't see the point of these warning at all.

  3. Ethan-Arrowood commented on Aug 18, 2023

    @Ethan-Arrowood
    Contributor

    I'm going to try to implement this feature soon. Probably will be a couple weeks because of other responsibilities, but unless someone beats me to it I'll try to get this implemented 🚀

  4. added a commit that references this issue on Nov 23, 2023
  5. added a commit that references this issue on Nov 27, 2023
  6. added 2 commits that reference this issue on Nov 29, 2023
  7. moved this from Awaiting Triage to Done in Node.js feature requestson Jun 29, 2024
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.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions