Skip to content
Related Articles

Related Articles

Improve Article

Node.js process.exit() Method

  • Last Updated : 25 Mar, 2021

The process.exit() method is used to end the process which is running at the same time with an exit code in NodeJS.

Syntax:

process.exit( code )

Parameter:  This function accepts single parameter as mentioned above and described below:

  • Code: It can be either 0 or 1. 0 means end the process without any kind of failure and 1 means end the process with some failure.

Return value: It does not return any value.

As it is the predefined module, so we don’t have to install it in our directory.



How to implement in code?

  1. Create a file with name index.js
  2. Create a variable with the name process and require the ‘process’ module in it.
  3. Create an infinite loop to check the functionality of .exit().

Case 1: Without using process.exit() method:

index.js




// Importing process module
var process = require('process');
  
var a = 0;
  
// Infinte loop
while (a == 0) {
  
    // Printing statement
    console.log('GeeksforGeeks');
  
}

Run index.js file using below command:

node index.js

Output: In the above code, we have created an infinite loop that prints GeeksForGeeks until we have stopped the program manually.

Case-II: Using process.exit() method:

index.js




// Importing process module
var process = require('process');
var a = 0;
  
// Infinite loop
while (a == 0) {
  
    // Printing statement
    console.log('GeeksForGeeks');
      
    // Terminate the entire process
    process.exit(0);
}

Run index.js file using below command:

node index.js

Output: In the above code, we have used the same code as CASE-I but the only difference is we have used process.exit() function that automatically stops the NodeJS program when there is some problem with the code. In this case, the code prints GeeksForGeeks only for a single time.

Reference: https://nodejs.org/api/process.html#process_process_exit_code




My Personal Notes arrow_drop_up
Recommended Articles
Page :