Open In App

Node.js Process warning Event

Last Updated : 13 Oct, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

The ‘warning’ is an event of class Process within the process module which is emitted whenever Node.js emits a process warning.

Syntax:

Event: 'warning'

Parameters: This event does not accept any argument as a parameter.

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

 

Example 1: 

index.js




// Node.js program to demonstrate the  
// Process 'warning' Event
  
// Importing process module
const process = require('process');
  
// Intentionally emitted warning
process.emitWarning('something strange happened');
  
// Event 'warning' 
process.on('warning', (warning) => {
   console.warn("warning name - " + warning.name);
   console.warn("warning message - " + warning.message);
});


Run the index.js file using the following command:

node index.js

Output:

(node:8004) Warning: something strange happened
(Use `node --trace-warnings ...` to show where
the warning was created)
warning name - Warning
warning message - something strange happened

Example 2: 

index.js




// Node.js program to demonstrate the  
// Process 'warning' Event
  
// Importing process module
const process = require('process');
  
// Intentionally emitted warning
process.emitWarning('Running out of Storage');
  
// Event 'warning' 
process.on('warning', (warning) => {
  console.warn("warning stacktrace - " + warning.stack)
});


Run the index.js file using the following command:

node index.js

Output:

(node:13400) Warning: Running out of Storage
(Use `node –trace-warnings …` to show where the warning was created)
warning stacktrace – Warning: Running out of Storage
at Object.<anonymous> (F:\java\GFG.js:8:9)
at Module._compile (node:internal/modules/cjs/loader:1109:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1138:10)
at Module.load (node:internal/modules/cjs/loader:989:32)
at Function.Module._load (node:internal/modules/cjs/loader:829:14)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:76:12)
at node:internal/main/run_main_module:17:47

Reference: https://nodejs.org/dist/latest-v16.x/docs/api/process.html#process_event_warning



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads