事件:'warning'


【Event: 'warning'

  • warning <Error> 警告的关键属性有:
    • name <string> 警告的名称。默认值: 'Warning'
    • message <string> 系统提供的关于该警告的描述。
    • stack <string> 发出警告的代码位置的堆栈跟踪。

每当 Node.js 发出进程警告时,就会触发 'warning' 事件。

【The 'warning' event is emitted whenever Node.js emits a process warning.】

进程警告类似于错误,它描述了需要引起用户注意的异常情况。然而,警告并不属于正常的 Node.js 和 JavaScript 错误处理流程。当 Node.js 检测到可能导致应用性能下降、错误或安全漏洞的不良编码实践时,它可以发出警告。

【A process warning is similar to an error in that it describes exceptional conditions that are being brought to the user's attention. However, warnings are not part of the normal Node.js and JavaScript error handling flow. Node.js can emit warnings whenever it detects bad coding practices that could lead to sub-optimal application performance, bugs, or security vulnerabilities.】

import process from 'node:process';

process.on('warning', (warning) => {
  console.warn(warning.name);    // Print the warning name
  console.warn(warning.message); // Print the warning message
  console.warn(warning.stack);   // Print the stack trace
});const process = require('node:process');

process.on('warning', (warning) => {
  console.warn(warning.name);    // Print the warning name
  console.warn(warning.message); // Print the warning message
  console.warn(warning.stack);   // Print the stack trace
});

默认情况下,Node.js 会将进程警告打印到 stderr。可以使用 --no-warnings 命令行选项来抑制默认的控制台输出,但 process 对象仍会触发 'warning' 事件。目前,除了弃用警告之外,无法抑制特定类型的警告。要抑制弃用警告,请查看 --no-deprecation 标志。

【By default, Node.js will print process warnings to stderr. The --no-warnings command-line option can be used to suppress the default console output but the 'warning' event will still be emitted by the process object. Currently, it is not possible to suppress specific warning types other than deprecation warnings. To suppress deprecation warnings, check out the --no-deprecation flag.】

下面的示例说明了当向某个事件添加了过多监听器时,会打印到 stderr 的警告:

【The following example illustrates the warning that is printed to stderr when too many listeners have been added to an event:】

$ node
> events.defaultMaxListeners = 1;
> process.on('foo', () => {});
> process.on('foo', () => {});
> (node:38638) MaxListenersExceededWarning: Possible EventEmitter memory leak
detected. 2 foo listeners added. Use emitter.setMaxListeners() to increase limit 

相反,下面的示例关闭了默认的警告输出,并为 'warning' 事件添加了自定义处理程序:

【In contrast, the following example turns off the default warning output and adds a custom handler to the 'warning' event:】

$ node --no-warnings
> const p = process.on('warning', (warning) => console.warn('Do not do that!'));
> events.defaultMaxListeners = 1;
> process.on('foo', () => {});
> process.on('foo', () => {});
> Do not do that! 

--trace-warnings 命令行选项可用于使默认控制台警告输出包含警告的完整堆栈跟踪。

【The --trace-warnings command-line option can be used to have the default console output for warnings include the full stack trace of the warning.】

使用 --throw-deprecation 命令行标志启动 Node.js 会将自定义弃用警告作为异常抛出。

【Launching Node.js using the --throw-deprecation command-line flag will cause custom deprecation warnings to be thrown as exceptions.】

使用 --trace-deprecation 命令行标志将会将自定义弃用信息连同堆栈跟踪一起打印到 stderr

【Using the --trace-deprecation command-line flag will cause the custom deprecation to be printed to stderr along with the stack trace.】

使用 --no-deprecation 命令行选项将抑制所有自定义弃用的报告。

【Using the --no-deprecation command-line flag will suppress all reporting of the custom deprecation.】

*-deprecation 命令行标志只影响使用名称 'DeprecationWarning' 的警告。

【The *-deprecation command-line flags only affect warnings that use the name 'DeprecationWarning'.】