process.emitWarning(warning[, type[, code]][, ctor])
添加于:v6.0.0
参数
warning<string> | <Error> 要发出的警告。type<string>什么时候warning是一个String,type是用于类型发出警告。默认:'Warning'.code<string> 发出的警告实例的唯一标识符。ctor<Function> 当warning是String时,ctor是一个可选函数,用于限制生成的堆栈跟踪。 默认:process.emitWarning。
process.emitWarning() 方法可用于发出自定义或应用程序特定的进程警告。可以通过向  事件添加处理程序来监听这些。'warning' 
import { emitWarning } from 'node:process'; // Emit a warning using a string. emitWarning('Something happened!'); // Emits: (node: 56338) Warning: Something happened!const { emitWarning } = require('node:process'); // Emit a warning using a string. emitWarning('Something happened!'); // Emits: (node: 56338) Warning: Something happened!
import { emitWarning } from 'node:process'; // Emit a warning using a string and a type. emitWarning('Something Happened!', 'CustomWarning'); // Emits: (node:56338) CustomWarning: Something Happened!const { emitWarning } = require('node:process'); // Emit a warning using a string and a type. emitWarning('Something Happened!', 'CustomWarning'); // Emits: (node:56338) CustomWarning: Something Happened!
import { emitWarning } from 'node:process'; emitWarning('Something happened!', 'CustomWarning', 'WARN001'); // Emits: (node:56338) [WARN001] CustomWarning: Something happened!const { emitWarning } = require('node:process'); process.emitWarning('Something happened!', 'CustomWarning', 'WARN001'); // Emits: (node:56338) [WARN001] CustomWarning: Something happened!
在前面的每个示例中,Error 对象由 process.emitWarning() 在内部生成并传递给   处理程序。'warning' 
import process from 'node:process'; process.on('warning', (warning) => { console.warn(warning.name); console.warn(warning.message); console.warn(warning.code); console.warn(warning.stack); });const process = require('node:process'); process.on('warning', (warning) => { console.warn(warning.name); console.warn(warning.message); console.warn(warning.code); console.warn(warning.stack); });
如果 warning 作为 Error 对象传递,它将未经修改地传递给 'warning' 事件处理程序(并且可选的 type 、 code 和 ctor 参数将被忽略):
import { emitWarning } from 'node:process'; // Emit a warning using an Error object. const myWarning = new Error('Something happened!'); // Use the Error name property to specify the type name myWarning.name = 'CustomWarning'; myWarning.code = 'WARN001'; emitWarning(myWarning); // Emits: (node:56338) [WARN001] CustomWarning: Something happened!const { emitWarning } = require('node:process'); // Emit a warning using an Error object. const myWarning = new Error('Something happened!'); // Use the Error name property to specify the type name myWarning.name = 'CustomWarning'; myWarning.code = 'WARN001'; emitWarning(myWarning); // Emits: (node:56338) [WARN001] CustomWarning: Something happened!
如果 warning 不是字符串或 Error 对象,则会引发 TypeError。
虽然进程警告使用Error 对象,但进程警告机制不能替代正常的错误处理机制。
如果警告 type 是 'DeprecationWarning' ,则执行以下附加处理:
- 如果使用 
--throw-deprecation命令行 标志,则弃用警告将作为异常抛出,而不是作为事件发出。 - 如果使用
--no-deprecation命令行 标志,则会抑制弃用警告。 - 如果使用 
--trace-deprecation命令行 标志,则会将弃用警告与完整堆栈跟踪一起打印到stderr。 
避免重复警告#
作为最佳实践,每个进程只应发出一次警告。为此,请将emitWarning() 放在布尔值后面。
import { emitWarning } from 'node:process'; function emitMyWarning() { if (!emitMyWarning.warned) { emitMyWarning.warned = true; emitWarning('Only warn once!'); } } emitMyWarning(); // Emits: (node: 56339) Warning: Only warn once! emitMyWarning(); // Emits nothingconst { emitWarning } = require('node:process'); function emitMyWarning() { if (!emitMyWarning.warned) { emitMyWarning.warned = true; emitWarning('Only warn once!'); } } emitMyWarning(); // Emits: (node: 56339) Warning: Only warn once! emitMyWarning(); // Emits nothing
相关用法
- Node.js process.emitWarning(warning[, options])用法及代码示例
 - Node.js process.emitWarning()用法及代码示例
 - Node.js process.execPath用法及代码示例
 - Node.js process.execArgv用法及代码示例
 - Node.js process.env用法及代码示例
 - Node.js process.env()用法及代码示例
 - Node.js process.exitCode用法及代码示例
 - Node.js process.exit([code])用法及代码示例
 - Node.js process.exit()用法及代码示例
 - Node.js process.stdin用法及代码示例
 - Node.js process.arch()用法及代码示例
 - Node.js process.nextTick(callback[, ...args])用法及代码示例
 - Node.js process.noDeprecation用法及代码示例
 - Node.js process.setUncaughtExceptionCaptureCallback()用法及代码示例
 - Node.js process.getgid()用法及代码示例
 - Node.js process.setgid(id)用法及代码示例
 - Node.js process.chdir(directory)用法及代码示例
 - Node.js process.setgid()用法及代码示例
 - Node.js process.getuid()用法及代码示例
 - Node.js process.ppid用法及代码示例
 - Node.js process.report.reportOnSignal用法及代码示例
 - Node.js process.report.directory用法及代码示例
 - Node.js process.umask(mask)用法及代码示例
 - Node.js process.setgroups(groups)用法及代码示例
 - Node.js process.setegid(id)用法及代码示例
 
注:本文由纯净天空筛选整理自nodejs.org大神的英文原创作品 process.emitWarning(warning[, type[, code]][, ctor])。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
