'warning' 事件
warning
<Error> 警告的主要属性是:
每当 Node.js 触发进程警告时,则会触发 'warning'
事件。
进程警告类似于错误,因为其描述了引起用户注意的异常情况。 但是,警告不是正常 Node.js 和 JavaScript 错误处理流程的一部分。 Node.js 可以在检测到可能导致次优应用程序性能、错误或安全漏洞的不良编码实践时触发警告。
process.on('warning', (warning) => {
console.warn(warning.name); // 打印警告名称
console.warn(warning.message); // 打印警告信息
console.warn(warning.stack); // 打印堆栈跟踪
});
默认情况下,Node.js 会将进程警告打印到 stderr
。
--no-warnings
命令行选项可用于抑制默认控制台输出,但 'warning'
事件仍将由 process
对象触发。
以下示例说明了在向事件添加过多监听器时打印到 stderr
的警告:
$ 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'
事件添加自定义句柄:
$ 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
命令行选项可用于使警告的默认控制台输出包括警告的完整堆栈跟踪。
使用 --throw-deprecation
命令行标志启动 Node.js 将导致自定义弃用警告作为异常抛出。
使用 --trace-deprecation
命令行标志将导致自定义弃用与堆栈跟踪一起打印到 stderr
。
使用 --no-deprecation
命令行标志将抑制自定义弃用的所有报告。
*-deprecation
命令行标志仅影响使用名称 'DeprecationWarning'
的警告。
warning
<Error> Key properties of the warning are:
The 'warning'
event is emitted whenever Node.js emits a process warning.
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.
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
});
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.
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
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!
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.
Launching Node.js using the --throw-deprecation
command-line flag will
cause custom deprecation warnings to be thrown as exceptions.
Using the --trace-deprecation
command-line flag will cause the custom
deprecation to be printed to stderr
along with the stack trace.
Using the --no-deprecation
command-line flag will suppress all reporting
of the custom deprecation.
The *-deprecation
command-line flags only affect warnings that use the name
'DeprecationWarning'
.