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.