@@ -186,6 +186,120 @@ this, you can either attach a dummy `.catch(() => { })` handler to |
186 | 186 | `resource.loaded`, preventing the `'unhandledRejection'` event from being |
187 | 187 | emitted, or you can use the [`'rejectionHandled'`][] event. |
188 | 188 |
|
| 189 | +## Event: 'warning' |
| 190 | + |
| 191 | +Emitted whenever Node.js emits a process warning. |
| 192 | + |
| 193 | +A process warning is similar to an error in that it describes exceptional |
| 194 | +conditions that are being brought to the user's attention. However, warnings |
| 195 | +are not part of the normal Node.js and JavaScript error handling flow. |
| 196 | +Node.js can emit warnings whenever it detects bad coding practices that could |
| 197 | +lead to sub-optimal application performance, bugs or security vulnerabilities. |
| 198 | + |
| 199 | +The event handler for `'warning'` events is called with a single `warning` |
| 200 | +argument whose value is an `Error` object. There are three key properties that |
| 201 | +describe the warning: |
| 202 | + |
| 203 | +* `name` - The name of the warning (currently `Warning` by default). |
| 204 | +* `message` - A system-provided description of the warning. |
| 205 | +* `stack` - A stack trace to the location in the code where the warning was |
| 206 | + issued. |
| 207 | + |
| 208 | +```js |
| 209 | +process.on('warning', (warning) => { |
| 210 | + console.warn(warning.name); // Print the warning name |
| 211 | + console.warn(warning.message); // Print the warning message |
| 212 | + console.warn(warning.stack); // Print the stack trace |
| 213 | +}); |
| 214 | +``` |
| 215 | + |
| 216 | +By default, Node.js will print process warnings to `stderr`. The `--no-warnings` |
| 217 | +command-line option can be used to suppress the default console output but the |
| 218 | +`'warning'` event will still be emitted by the `process` object. |
| 219 | + |
| 220 | +The following example illustrates the warning that is printed to `stderr` when |
| 221 | +too many listeners have been added to an event |
| 222 | + |
| 223 | +``` |
| 224 | +$ node |
| 225 | +> event.defaultMaxListeners = 1; |
| 226 | +> process.on('foo', () => {}); |
| 227 | +> process.on('foo', () => {}); |
| 228 | +> (node:38638) Warning: Possible EventEmitter memory leak detected. 2 foo |
| 229 | +... listeners added. Use emitter.setMaxListeners() to increase limit |
| 230 | +``` |
| 231 | + |
| 232 | +In contrast, the following example turns off the default warning output and |
| 233 | +adds a custom handler to the `'warning'` event: |
| 234 | + |
| 235 | +``` |
| 236 | +$ node --no-warnings |
| 237 | +> var p = process.on('warning', (warning) => console.warn('Do not do that!')); |
| 238 | +> event.defaultMaxListeners = 1; |
| 239 | +> process.on('foo', () => {}); |
| 240 | +> process.on('foo', () => {}); |
| 241 | +> Do not do that! |
| 242 | +``` |
| 243 | + |
| 244 | +The `--trace-warnings` command-line option can be used to have the default |
| 245 | +console output for warnings include the full stack trace of the warning. |
| 246 | + |
| 247 | +### Emitting custom warnings |
| 248 | + |
| 249 | +The [`process.emitWarning()`][process_emit_warning] method can be used to issue |
| 250 | +custom or application specific warnings. |
| 251 | + |
| 252 | +```js |
| 253 | +// Emit a warning using a string... |
| 254 | +process.emitWarning('Something happened!'); |
| 255 | + // Prints: (node 12345) Warning: Something happened! |
| 256 | + |
| 257 | +// Emit a warning using an object... |
| 258 | +process.emitWarning('Something Happened!', 'CustomWarning'); |
| 259 | + // Prints: (node 12345) CustomWarning: Something happened! |
| 260 | + |
| 261 | +// Emit a warning using a custom Error object... |
| 262 | +class CustomWarning extends Error { |
| 263 | + constructor(message) { |
| 264 | + super(message); |
| 265 | + this.name = 'CustomWarning'; |
| 266 | + Error.captureStackTrace(this, CustomWarning); |
| 267 | + } |
| 268 | +} |
| 269 | +const myWarning = new CustomWarning('Something happened!'); |
| 270 | +process.emitWarning(myWarning); |
| 271 | + // Prints: (node 12345) CustomWarning: Something happened! |
| 272 | +``` |
| 273 | + |
| 274 | +### Emitting custom deprecation warnings |
| 275 | + |
| 276 | +Custom deprecation warnings can be emitted by setting the `name` of a custom |
| 277 | +warning to `DeprecationWarning`. For instance: |
| 278 | + |
| 279 | +```js |
| 280 | +process.emitWarning('This API is deprecated', 'DeprecationWarning'); |
| 281 | +``` |
| 282 | + |
| 283 | +Or, |
| 284 | + |
| 285 | +```js |
| 286 | +const err = new Error('This API is deprecated'); |
| 287 | +err.name = 'DeprecationWarning'; |
| 288 | +process.emitWarning(err); |
| 289 | +``` |
| 290 | + |
| 291 | +Launching Node.js using the `--throw-deprecation` command line flag will |
| 292 | +cause custom deprecation warnings to be thrown as exceptions. |
| 293 | + |
| 294 | +Using the `--trace-deprecation` command line flag will cause the custom |
| 295 | +deprecation to be printed to `stderr` along with the stack trace. |
| 296 | + |
| 297 | +Using the `--no-deprecation` command line flag will suppress all reporting |
| 298 | +of the custom deprecation. |
| 299 | + |
| 300 | +The `*-deprecation` command line flags only affect warnings that use the name |
| 301 | +`DeprecationWarning`. |
| 302 | + |
189 | 303 | ## Exit Codes |
190 | 304 |
|
191 | 305 | Node.js will normally exit with a `0` status code when no more async |
@@ -457,6 +571,92 @@ console.log(process.env.TEST); |
457 | 571 | // => undefined |
458 | 572 | ``` |
459 | 573 |
|
| 574 | +## process.emitWarning(warning[, name][, ctor]) |
| 575 | + |
| 576 | +* `warning` {String | Error} The warning to emit. |
| 577 | +* `name` {String} When `warning` is a String, `name` is the name to use |
| 578 | + for the warning. Default: `Warning`. |
| 579 | +* `ctor` {Function} When `warning` is a String, `ctor` is an optional |
| 580 | + function used to limit the generated stack trace. Default |
| 581 | + `process.emitWarning` |
| 582 | + |
| 583 | +The `process.emitWarning()` method can be used to emit custom or application |
| 584 | +specific process warnings. These can be listened for by adding a handler to the |
| 585 | +[`process.on('warning')`][process_warning] event. |
| 586 | + |
| 587 | +```js |
| 588 | +// Emit a warning using a string... |
| 589 | +process.emitWarning('Something happened!'); |
| 590 | + // Emits: (node: 56338) Warning: Something happened! |
| 591 | +``` |
| 592 | + |
| 593 | +``` |
| 594 | +// Emit a warning using a string and a name... |
| 595 | +process.emitWarning('Something Happened!', 'CustomWarning'); |
| 596 | + // Emits: (node:56338) CustomWarning: Something Happened! |
| 597 | +``` |
| 598 | + |
| 599 | +In each of the previous examples, an `Error` object is generated internally by |
| 600 | +`process.emitWarning()` and passed through to the |
| 601 | +[`process.on('warning')`][process_warning] event. |
| 602 | + |
| 603 | +``` |
| 604 | +process.on('warning', (warning) => { |
| 605 | + console.warn(warning.name); |
| 606 | + console.warn(warning.message); |
| 607 | + console.warn(warning.stack); |
| 608 | +}); |
| 609 | +``` |
| 610 | + |
| 611 | +If `warning` is passed as an `Error` object, it will be passed through to the |
| 612 | +`process.on('warning')` event handler unmodified (and the optional `name` |
| 613 | +and `ctor` arguments will be ignored): |
| 614 | + |
| 615 | +``` |
| 616 | +// Emit a warning using an Error object... |
| 617 | +const myWarning = new Error('Warning! Something happened!'); |
| 618 | +myWarning.name = 'CustomWarning'; |
| 619 | +
|
| 620 | +process.emitWarning(myWarning); |
| 621 | + // Emits: (node:56338) CustomWarning: Warning! Something Happened! |
| 622 | +``` |
| 623 | + |
| 624 | +A `TypeError` is thrown if `warning` is anything other than a string or `Error` |
| 625 | +object. |
| 626 | + |
| 627 | +Note that while process warnings use `Error` objects, the process warning |
| 628 | +mechanism is **not** a replacement for normal error handling mechanisms. |
| 629 | + |
| 630 | +The following additional handling is implemented if the warning `name` is |
| 631 | +`DeprecationWarning`: |
| 632 | + |
| 633 | +* If the `--throw-deprecation` command-line flag is used, the deprecation |
| 634 | + warning is thrown as an exception rather than being emitted as an event. |
| 635 | +* If the `--no-deprecation` command-line flag is used, the deprecation |
| 636 | + warning is suppressed. |
| 637 | +* If the `--trace-deprecation` command-line flag is used, the deprecation |
| 638 | + warning is printed to `stderr` along with the full stack trace. |
| 639 | + |
| 640 | +### Avoiding duplicate warnings |
| 641 | + |
| 642 | +As a best practice, warnings should be emitted only once per process. To do |
| 643 | +so, it is recommended to place the `emitWarning()` behind a simple boolean |
| 644 | +flag as illustrated in the example below: |
| 645 | + |
| 646 | +``` |
| 647 | +var warned = false; |
| 648 | +function emitMyWarning() { |
| 649 | + if (!warned) { |
| 650 | + process.emitWarning('Only warn once!'); |
| 651 | + warned = true; |
| 652 | + } |
| 653 | +} |
| 654 | +emitMyWarning(); |
| 655 | + // Emits: (node: 56339) Warning: Only warn once! |
| 656 | +emitMyWarning(); |
| 657 | + // Emits nothing |
| 658 | +``` |
| 659 | + |
460 | 660 | ## process.execArgv |
461 | 661 |
|
462 | 662 | This is the set of Node.js-specific command line options from the |
@@ -1098,4 +1298,6 @@ Will print something like: |
1098 | 1298 | [Signal Events]: #process_signal_events |
1099 | 1299 | [Stream compatibility]: stream.html#stream_compatibility_with_older_node_js_versions |
1100 | 1300 | [the tty docs]: tty.html#tty_tty |
1101 | | -[`JSON.stringify()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify |
| 1301 | +[`JSON.stringify()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify |
| 1302 | +[process_warning]: #process_event_warning |
| 1303 | +[process_emit_warning]: #process_emitwarning_warning_name_ctor |