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
const process = require( 'process' );
process.emitWarning( 'something strange happened' );
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
const process = require( 'process' );
process.emitWarning( 'Running out of Storage' );
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