事件:'warning'
¥Event: 'warning'
-
warning
<Error> 警告的主要属性是:¥
warning
<Error> Key properties of the warning are:-
name
<string> 警告的名称。默认值:'Warning'
。¥
name
<string> The name of the warning. Default:'Warning'
. -
message
<string> 系统提供的警告描述。¥
message
<string> A system-provided description of the warning. -
stack
<string> 代码中触发警告的位置的堆栈跟踪。¥
stack
<string> A stack trace to the location in the code where the warning was issued.
-
每当 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
命令行选项可用于抑制默认控制台输出,但 'warning'
事件仍将由 process
对象触发。目前,无法抑制除弃用警告之外的特定警告类型。要抑制弃用警告,请检查 --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'
.