Skip to main content
Process.emitWarning - node__process.d.ts - Node documentation
method Process.emitWarning

Usage in Deno

```typescript import { type Process } from "node:node__process.d.ts"; ```
Process.emitWarning(
warning: string | Error,
ctor?: Function,
): void
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. ```js import { emitWarning } from 'node:process'; // Emit a warning using a string. emitWarning('Something happened!'); // Emits: (node: 56338) Warning: Something happened! ``` ```js import { emitWarning } from 'node:process'; // Emit a warning using a string and a type. emitWarning('Something Happened!', 'CustomWarning'); // Emits: (node:56338) CustomWarning: Something Happened! ``` ```js import { emitWarning } from 'node:process'; emitWarning('Something happened!', 'CustomWarning', 'WARN001'); // Emits: (node:56338) [WARN001] CustomWarning: Something happened! ```js In each of the previous examples, an `Error` object is generated internally by `process.emitWarning()` and passed through to the `'warning'` handler. ```js 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' }); ``` 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): ```js 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! ``` 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-deprecation` command-line flag is used, the deprecation warning is thrown as an exception rather than being emitted as an event. * If the `--no-deprecation` command-line flag is used, the deprecation warning is suppressed. * If the `--trace-deprecation` command-line flag is used, the deprecation warning is printed to `stderr` along with the full stack trace.

Parameters

warning: string | Error
The warning to emit.
optional
ctor: Function

Return Type

void
Process.emitWarning(
warning: string | Error,
type?: string,
ctor?: Function,
): void

Parameters

warning: string | Error
optional
type: string
optional
ctor: Function

Return Type

void
Process.emitWarning(
warning: string | Error,
type?: string,
code?: string,
ctor?: Function,
): void

Parameters

warning: string | Error
optional
type: string
optional
code: string
optional
ctor: Function

Return Type

void
Process.emitWarning(
warning: string | Error,
): void

Parameters

warning: string | Error
optional
options: EmitWarningOptions

Return Type

void