by Sai gowtham

How to exit the process in Node.js

In this tutorial, we are going to learn about two different ways to exit the process in node.js.

Manually exiting the process

If you want to exit the process manually from a command line, you need to use ctrl+c.

Programmatically exiting the process

If you want to exit the process from a node.js program, you can use the process.exit() method which is a global object that terminates the process synchronously.

process.exit();

The process.exit() method also accepts an exit code, if we pass 1 it exits with a failure code.

process.exit(1);

If there is no status code provided, it exits with a success code 0.

You can also use process.exitCode property, to set the exit code.

process.exitCode = 1;

This code will be used by the process, when a process exits gracefully after the all pending work is finished in the event loop or a process is exited using process.exit() method without specifying a code.

Top Udemy Courses

JavaScript - The Complete Guide 2020 (Beginner + Advanced)
JavaScript - The Complete Guide 2020 (Beginner + Advanced)
45,614 students enrolled
52 hours of video content
View Course
React - The Complete Guide (incl Hooks, React Router, Redux)
React - The Complete Guide (incl Hooks, React Router, Redux)
284,472 students enrolled
40 hours of video content
View Course
Vue JS 2 - The Complete Guide (incl. Vue Router & Vuex)
Vue JS 2 - The Complete Guide (incl. Vue Router & Vuex)
130,921 students enrolled
21 hours of video content
View Course