process.emitWarning(warning[, options])
-
options<Object>-
type<string> 当warning是String时,type是用于触发的警告类型的名称。默认值:'Warning'。¥
type<string> Whenwarningis aString,typeis the name to use for the type of warning being emitted. Default:'Warning'. -
code<string> 触发的警告实例的唯一标识符。¥
code<string> A unique identifier for the warning instance being emitted. -
ctor<Function> 当warning为String时,ctor是可选函数,用于限制生成的堆栈跟踪。默认值:process.emitWarning。¥
ctor<Function> Whenwarningis aString,ctoris an optional function used to limit the generated stack trace. Default:process.emitWarning. -
detail<string> 要包含在错误中的额外文本。¥
detail<string> Additional text to include with the error.
-
process.emitWarning() 方法可用于触发自定义或特定于应用的进程警告。这些可以通过向 'warning' 事件添加句柄来监听。
¥The process.emitWarning() method can be used to emit custom or application
specific process warnings. These can be listened for by adding a handler to the
'warning' event.
import { emitWarning } from 'node:process';
// Emit a warning with a code and additional detail.
emitWarning('Something happened!', {
code: 'MY_WARNING',
detail: 'This is some additional information',
});
// Emits:
// (node:56338) [MY_WARNING] Warning: Something happened!
// This is some additional informationconst { emitWarning } = require('node:process');
// Emit a warning with a code and additional detail.
emitWarning('Something happened!', {
code: 'MY_WARNING',
detail: 'This is some additional information',
});
// Emits:
// (node:56338) [MY_WARNING] Warning: Something happened!
// This is some additional information在此示例中,Error 对象由 process.emitWarning() 在内部生成并传给 'warning' 句柄。
¥In this example, an Error object is generated internally by
process.emitWarning() and passed through to the
'warning' handler.
import process from 'node:process';
process.on('warning', (warning) => {
console.warn(warning.name); // 'Warning'
console.warn(warning.message); // 'Something happened!'
console.warn(warning.code); // 'MY_WARNING'
console.warn(warning.stack); // Stack trace
console.warn(warning.detail); // 'This is some additional information'
});const process = require('node:process');
process.on('warning', (warning) => {
console.warn(warning.name); // 'Warning'
console.warn(warning.message); // 'Something happened!'
console.warn(warning.code); // 'MY_WARNING'
console.warn(warning.stack); // Stack trace
console.warn(warning.detail); // 'This is some additional information'
});如果 warning 作为 Error 对象传入,则忽略 options 参数。
¥If warning is passed as an Error object, the options argument is ignored.