Node.js Reference ยท process
Node.js process.emitWarning() method
Reference for process.emitWarning() in Node.js: emitting warnings with a type, code and detail, listening to the warning event, and the flags that control output.
process.emitWarning() raises a warning: a message that something is deprecated, risky or unusual, but not serious enough to stop the program. By default Node.js prints it once to standard error, in the form (node:PID) Warning: text, and the program keeps running.
Every warning also triggers a 'warning' event on process, so tools can collect or format them. Node itself uses the same mechanism for deprecations, experimental features and listener-leak notices.
process.emitWarning('Cache is almost full', 'CapacityWarning', 'APP001');
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.
Syntax
process.emitWarning(warning[, type[, code]][, ctor]) process.emitWarning(warning[, options])
| Name | Description |
|---|---|
warning |
A string, or an Error object whose name becomes the warning type. |
type |
Warning name, default 'Warning'. 'DeprecationWarning' has special handling. |
code |
A short identifier such as 'APP001'. Shown in brackets in the printed message. |
ctor |
A function used to cut the stack trace shown with the warning. |
options.detail |
Extra text printed on a new line after the message. |
Returns: undefined. The 'warning' event is emitted on the next tick, not synchronously.
Errors / edge cases: a first argument that is neither a string nor an Error throws ERR_INVALID_ARG_TYPE. Invalid type or code types throw the same.
Usage notes
- Deprecations are special. A
DeprecationWarningis not printed if--no-deprecationis set, is thrown as an exception with--throw-deprecation, and shows a trace with--trace-deprecation. - Flags:
--no-warnings(or the environment variableNODE_NO_WARNINGS=1) silences all output while the event still fires, and--trace-warningsprints stack traces. - Print once. Node has no built-in deduplication for your own warnings. Use a
Set, as inwarnOnce(), or you flood the logs. - Use
code. It lets users filter and search for the warning, and lets tests check it without matching message text. - Choose warnings over
console.warn()for things library users may want to intercept or mute: apps can listen to the event, send them to monitoring, or silence them with a flag. - Typical built-in warnings:
MaxListenersExceededWarning,ExperimentalWarning,DeprecationWarningandTimeoutNegativeWarning. - Removing the default listener with
removeAllListeners('warning')also stops stderr output, as the demos above do. This is for tools and tests; in an application prefer--no-warnings. util.deprecate(fn, msg, code)wraps a function so the first call emits a deprecation warning.