@@ -706,14 +706,15 @@ console.log(process.env.test);
706706// => 1
707707```
708708
709- ## process.emitWarning(warning[ , name ] [ , ctor ] )
709+ ## process.emitWarning(warning[ , type [ , code ] ][ , ctor ] )
710710<!-- YAML
711711added: v6.0.0
712712-->
713713
714714* ` warning ` {String | Error} The warning to emit.
715- * ` name ` {String} When ` warning ` is a String, ` name ` is the name to use
716- for the warning. Default: ` Warning ` .
715+ * ` type ` {String} When ` warning ` is a String, ` type ` is the name to use
716+ for the * type* of warning being emitted. Default: ` Warning ` .
717+ * ` code ` {String} A unique identifier for the warning instance being emitted.
717718* ` ctor ` {Function} When ` warning ` is a String, ` ctor ` is an optional
718719 function used to limit the generated stack trace. Default
719720 ` process.emitWarning `
@@ -729,11 +730,16 @@ process.emitWarning('Something happened!');
729730```
730731
731732``` js
732- // Emit a warning using a string and a name ...
733+ // Emit a warning using a string and a type ...
733734process .emitWarning (' Something Happened!' , ' CustomWarning' );
734735// Emits: (node:56338) CustomWarning: Something Happened!
735736```
736737
738+ ``` js
739+ process .emitWarning (' Something happened!' , ' CustomWarning' , ' WARN001' );
740+ // Emits: (node:56338) CustomWarning [WARN001]: Something Happened!
741+ ```
742+
737743In each of the previous examples, an ` Error ` object is generated internally by
738744` process.emitWarning() ` and passed through to the
739745[ ` process.on('warning') ` ] [ process_warning ] event.
@@ -742,21 +748,24 @@ In each of the previous examples, an `Error` object is generated internally by
742748process .on (' warning' , (warning ) => {
743749 console .warn (warning .name );
744750 console .warn (warning .message );
751+ console .warn (warning .code );
745752 console .warn (warning .stack );
746753});
747754```
748755
749756If ` warning ` is passed as an ` Error ` object, it will be passed through to the
750- ` process.on('warning') ` event handler unmodified (and the optional ` name `
751- and ` ctor ` arguments will be ignored):
757+ ` process.on('warning') ` event handler unmodified (and the optional ` type ` ,
758+ ` code ` and ` ctor ` arguments will be ignored):
752759
753760``` js
754761// Emit a warning using an Error object...
755762const myWarning = new Error (' Warning! Something happened!' );
763+ // Use the Error name property to specify the type name
756764myWarning .name = ' CustomWarning' ;
765+ myWarning .code = ' WARN001' ;
757766
758767process .emitWarning (myWarning);
759- // Emits: (node:56338) CustomWarning: Warning! Something Happened!
768+ // Emits: (node:56338) CustomWarning [WARN001] : Warning! Something Happened!
760769```
761770
762771A ` TypeError ` is thrown if ` warning ` is anything other than a string or ` Error `
@@ -765,7 +774,7 @@ object.
765774Note that while process warnings use ` Error ` objects, the process warning
766775mechanism is ** not** a replacement for normal error handling mechanisms.
767776
768- The following additional handling is implemented if the warning ` name ` is
777+ The following additional handling is implemented if the warning ` type ` is
769778` DeprecationWarning ` :
770779
771780* If the ` --throw-deprecation ` command-line flag is used, the deprecation
0 commit comments