Skip to content
Related Articles
Open in App
Not now

Related Articles

Node.js process.exitCode Property

Improve Article
Save Article
  • Last Updated : 01 Apr, 2021
Improve Article
Save Article

 There are two ways that are generally used  to terminate a Node.js program which are using process.exit() or process.exitCode variable. In this post, we have discussed about process.exitCode 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 exitcode to process.exit().

Syntax

  process.exitCode = value

 Here value means exitcode value.

Example 1: One way is to pass exitcode value to process.exit() function. Exit code 1 is used when unhandled fatal exceptions occur that was 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()
  
var a=10;
var 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 allows the Node.js program to exit on its own without leaving further calls for the future.

Javascript




const express = require('express')
const app = express()
  
var a=10;
var b=20;
  
if(a==10){
  console.log(a)
  process.exitCode=0;
}
  
console.log(b);
  
app.listen(3000, () => 
console.log('Server ready'))


Output:


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!