The ‘beforeExit’ event is emitted when Node.js empties its event loop and has no additional work to schedule. Normally, the Node.js process will exit when there is no work scheduled, but a listener registered on the ‘beforeExit’ event can make asynchronous calls, and thereby cause the Node.js process to continue.
process.on('beforeExit', (code) => { console.log('Process beforeExit event with code: ', code);});process.on('exit', (code) => { console.log('Process exit event with code: ', code);});console.log('This message is displayed first.');
The ‘exit’ event is emitted when the Node.js process is about to exit as a result of either:
The process.exit() method being called explicitly
The Node.js event loop no longer having any additional work to perform
process.on('exit', (code) => { console.log(`About to exit with code: ${code}`);});
Listener functions MUST only perform synchronous operations. The Node.js process will exit immediately after calling the ‘exit’ event listeners causing any additional work still queued in the event loop to be abandoned.
Indicates if the exception originates from an unhandled rejection or from a synchronous error. Can either be ‘uncaughtException’ or ‘unhandledRejection’.
The ‘uncaughtException’ event is emitted when an uncaught JavaScript exception bubbles all the way back to the event loop.
process.on('uncaughtException', (err, origin) => { fs.writeSync( process.stderr.fd, `Caught exception: ${err}\n` + `Exception origin: ${origin}\n` );});setTimeout(() => { console.log('This will still run.');}, 500);// Intentionally cause an exception, but don't catch it.nonexistentFunc();console.log('This will not run.');
‘uncaughtException’ is a crude mechanism for exception handling intended to be used only as a last resort. The event should not be used as an equivalent to On Error Resume Next. The correct use of ‘uncaughtException’ is to perform synchronous cleanup of allocated resources before shutting down the process.
The ‘unhandledRejection’ event is emitted whenever a Promise is rejected and no error handler is attached to the promise within a turn of the event loop.
process.on('unhandledRejection', (reason, promise) => { console.log('Unhandled Rejection at:', promise, 'reason:', reason);});somePromise.then((res) => { return reportToUser(JSON.pasre(res)); // Note the typo}); // No .catch() or .then()
The operating system CPU architecture for which the Node.js binary was compiled. Possible values: ‘arm’, ‘arm64’, ‘ia32’, ‘loong64’, ‘mips’, ‘mipsel’, ‘ppc64’, ‘riscv64’, ‘s390’, ‘s390x’, ‘x64’.
console.log(`This processor architecture is ${process.arch}`);
An array containing the command-line arguments passed when the Node.js process was launched.
The first element will be process.execPath. The second element will be the path to the JavaScript file being executed. The remaining elements will be any additional command-line arguments.
The operating system platform on which the Node.js process is running. Possible values: ‘aix’, ‘darwin’, ‘freebsd’, ‘linux’, ‘openbsd’, ‘sunos’, ‘win32’.
console.log(`This platform is ${process.platform}`);
process.nextTick() adds callback to the “next tick queue”. This queue is fully drained after the current operation on the JavaScript stack runs to completion and before the event loop is allowed to continue.
Signal events will be emitted when the Node.js process receives a signal. Please refer to signal(7) for a listing of standard POSIX signal names such as ‘SIGINT’, ‘SIGHUP’, etc.
process.stdin.resume();process.on('SIGINT', () => { console.log('Received SIGINT. Press Control-D to exit.');});// Using a single function to handle multiple signalsfunction handle(signal) { console.log(`Received ${signal}`);}process.on('SIGINT', handle);process.on('SIGTERM', handle);