Node.js process.exitCode Property
There are two ways that are generally used to terminate a Node.js program which is using process.exit() or process.exitCode variable. In this post, we have discussed the process. exit code variable. It is an integer that represents the code passed to the process.exit() function or when the process exits on its own. It allows the Node.js program to exit naturally and avoids the Node program to do additional work around event loop scheduling, and it is much safer than passing exit code to process.exit().
Syntax:
process.exitCode = value
Here value means exit code value.
Example 1: One way is to pass the exit code value to process.exit() function. Exit code 1 is used when unhandled fatal exceptions occur that were not handled whereas Exit code 0 is used to terminate when no more async operations are happening.
Javascript
const express = require( 'express' ) const app = express() let a = 10; let b = 20; if (a == 10) { console.log(a) process.exitCode(1); } console.log(b); app.listen(3000, () => console.log( 'Server ready' )) |
Output:
Example 2: Another way is to set process.exitCode value which will allow the Node.js program to exit on its own without leaving further calls for the future.
Javascript
const express = require( 'express' ) const app = express() let a = 10; let b = 20; if (a == 10) { console.log(a) process.exitCode = 0; } console.log(b); app.listen(3000, () => console.log( 'Server ready' )) |
Output:
Please Login to comment...