process.emitWarning(warning, type?, code?, ctor?): void
stringwarning is a String, type is the name to use
for the type of warning being emitted. Default: 'Warning'.stringFunctionwarning is a String, ctor is an optional
function used to limit the generated stack trace. Default:
process.emitWarning.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 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!
In each of the previous examples, 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); console.warn(warning.message); console.warn(warning.code); console.warn(warning.stack); });
process.on('warning', (warning) => { console.warn(warning.name); console.warn(warning.message); console.warn(warning.code); console.warn(warning.stack); });
If warning is passed as an Error object, it will be passed through to the
'warning' event handler unmodified (and the optional type,
code and ctor arguments will be ignored):
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!
A TypeError is thrown if warning is anything other than a string or Error
object.
While process warnings use Error objects, the process warning
mechanism is not a replacement for normal error handling mechanisms.
The following additional handling is implemented if the warning type is
'DeprecationWarning':
- If the
--throw-deprecationcommand-line flag is used, the deprecation warning is thrown as an exception rather than being emitted as an event. - If the
--no-deprecationcommand-line flag is used, the deprecation warning is suppressed. - If the
--trace-deprecationcommand-line flag is used, the deprecation warning is printed tostderralong with the full stack trace.
As a best practice, warnings should be emitted only once per process. To do
so, place the emitWarning() behind a boolean.
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 nothing
const { 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