Skip to content
Related Articles

Related Articles

Node.js process.emitWarning() Method

View Discussion
Improve Article
Save Article
Like Article
  • Last Updated : 25 May, 2021

The process.emitWarning() method is an inbuilt application programming interface of the process module which is used to send custom or application-specific process warnings.

Syntax:

process.emitWarning(warning[, options])

Parameters: This function accepts the following parameter:

  • warning: It is used to denote the warning which is to be emitted.
  • options: It is an optional parameter, and it can have the following properties:
    • type: It is used to denote the type of warning being emitted when the warning is in string form.
    • code: It is used to denote a unique identifier for the warning instance being emitted.
    • ctor: It is an optional function used to limit the generated stack trace when the warning is in string form.
    • detail: It is used to add the additional text to include with the error.

Return Value: This event returns nothing but a callback function for further operation.

 

Example 1: The process.emitWarning() method passes an Error object to the warning handler while listening to the process.

index.js




console.log("Start ...");
  
// Use setInterval to keep the process running
setInterval(() => {
    console.log("Working ...");
}, 1000);
  
setTimeout(() => {
    process.emitWarning('Something happened!', {
        code: 'Custom_Warning',
        detail: 'Additional information about warning'
    });
}, 4000);
  
// Start listening to the process
process.on('warning', (warning) => {
  
    // If there is a warning then print
    // it and stop the process
    if (warning) {
        console.log(warning);
        process.exit(0);
    }
});


Run the index.js file using the following command:

node index.js

Output:

Start …
Working …
Working …
Working …
(node:12621) [Custom_Warning] Warning: Something happened!
Additional information about warning
(Use `node –trace-warnings …` to show where the warning was created)
Warning: Something happened!
at Timeout._onTimeout (/home/aditya/Programming/GFG/New/EmitWarning.js:9:13)
at listOnTimeout (node:internal/timers:556:17)
at processTimers (node:internal/timers:499:7) {
code: ‘Custom_Warning’,
detail: ‘Additional information about warning’
}

Example 2: Emit multiple warnings and take action based on the warning type.

index.js




console.log("Start ...");
  
// Use setInterval to keep the process running
setInterval(() => {
    console.log("Working ...");
}, 1000);
  
setTimeout(() => {
    process.emitWarning('Something happened!', {
        code: 'Custom_Warning',
        detail: 'Additional information about warning'
    });
}, 4000);
  
setTimeout(() => {
    process.emitWarning('Something needs to be fixed!', {
        type: 'IMPORTANT',
        code: '007',
        detail: 'Can not proceed further!'
    });
}, 6000);
  
// Start listening to the process
process.on('warning', (warning) => {
  
    // If there is an important warning
    // stop the process
    // warning.name = type
    if (warning.name === 'IMPORTANT') {
        console.log('Fix this ASAP!')
        process.exit(0);
    }
});


Run the index.js file using the following command:

node index.js

Output:

Start …
Working …
Working …
Working …
(node:12893) [Custom_Warning] Warning: Something happened!
Additional information about warning
(Use `node –trace-warnings …` to show where the warning was created)
Working …
Working …
(node:12893) [007] IMPORTANT: Something needs to be fixed!
Can not proceed further!
Fix this ASAP!

Example 3: Passing an Error object as a warning instead of a string. Notice if the Error object is being passed then the optional parameters get ignored.

index.js




console.log("Start ...");
  
// Use setInterval to keep the process running
setInterval(() => {
    console.log("Working ...");
}, 1000);
  
setTimeout(() => {
    process.emitWarning(new Error('Whoops!'), {
        code: 'Custom_Warning',
        detail: 'Additional information about warning'
    });
}, 4000);
  
// Start listening to the process
process.on('warning', (warning) => {
  
    // If there is a warning then print
    // it and stop the process
    if (warning) {
        console.warn(warning);
        process.exit(0);
    }
});


Run the index.js file using the following command:

node index.js

Output:

Start …
Working …
Working …
Working …
(node:13232) Error: Whoops!
(Use `node –trace-warnings …` to show where the warning was created)
Error: Whoops!
at Timeout._onTimeout (/home/aditya/Programming/GFG/New/EmitWarning.js:9:25)
at listOnTimeout (node:internal/timers:556:17)
at processTimers (node:internal/timers:499:7)

Reference: https://nodejs.org/api/process.html#process_process_emitwarning_warning_type_code_ctor


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!