In this tutorial we will show you the various ways of how to exit Node.js programs. You need to understand first that Node.js works on a single thread or main process. You can spawn additional child processes to handle extra work. Exiting the main process lets us exit from Node.
While there are many ways to exit from Node, some ways are better than others for certain situations, like if you're running a REPL or not. We'll explain this in more detail throughout the article.
Letting a Script Exit Implicitly
Exiting from a Node.js program started at the command line can be as simple as waiting for the script to finish executing. Implicitly, the Node.js process will exit when it reaches the end of the script.
You can see this by running the following scipt:
// batman.js
console.log('Batman begins');
process.on('exit', function(code) {
return console.log(`About to exit with code ${code}`);
});
Run the program with the command node batman.js
, and you will see it output the first statement about Batman. In addition, the "exit" callback fires, resulting in a print out of the message about exiting, and an exit code. You should see something similar to the following:
$ node batman.js
Batman begins
About to exit with code 0
Note that pending events and loops will block program exit. Add this repeating function at the end of the above script.
// batman.js
// ...
setInterval((function() {
return console.log('I\'m Batman!');
}), 1000);
Run it again. This time, the program does not exit, because the repeating setInterval
function blocks Node.js from exiting. Your output will look similar to this:
$ node batman.js
Batman begins
I'm Batman!
I'm Batman!
I'm Batman!
...
Where the "I'm Batman!" phrase is continuously printed until you forcefully exit, like with Ctrl + C
, or close your terminal.
Using process.exit()
We can exit Node.js programs using the explicit process.exit
function call. The process.exit
function exits from the current Node.js process. It takes an exit code, which is an integer.
The process
object is a global variable that lets us manage the current Node.js process. Since it is a global, we can access it from anywhere inside a Node.js program without using require
to import it.
Let us update the last program which does not exit because of the setInterval
function. This time, we will force it to exit using a timeout
after 5 seconds have elapsed. Add the following function to the same 'batman.js' script from before.
// batman.js
// ...
setTimeout((function() {
return process.exit(22);
}), 5000);
When you run node batman.js
, you will notice that this time, the program runs for a limited time and then exits with an exit code of 22.
node batman.js
Batman begins
I'm Batman!
I'm Batman!
I'm Batman!
I'm Batman!
About to exit with code 22
Using process.exit
works for exiting from the REPL (which we'll see later) as well as for terminating running Node.js programs or scripts.
Node.js interprets non-zero codes as failure, and an exit code of 0 as success.
Exiting Node.js using process.kill()
We can also exit Node.js using process.kill
to kill the running Node.js process. The difference between this and process.exit
is that process.kill
takes the pid, or process id, of the process we want to kill, as well as an optional signal that we want to send to the process. This means we can send signals to kill processes other than the main Node.js process. This is useful in highly concurrent applications with many running processes.
To kill the main Node process, we just pass the pid of the main process.
To see this in operation, replace the setTimeout
function in our previous code example with this version that uses process.kill
.
// batman.js
// ...
setTimeout((function() {
return process.kill(process.pid);
}), 5000);
The program exits on schedule as before, after some of the same statements are printed. This time the pid is also printed to the console (yours may be different than 15):
$ node batman.js
Batman begins
I'm Batman!
I'm Batman!
I'm Batman!
I'm Batman!
Terminated: 15
This method also works in the REPL as well as in Node.js programs.
How to Exit Node.js using process.abort
Likewise, we can use process.abort
to exit Node.js. This method works in the REPL as well as scripts and applications.
The difference between process.abort
, process.kill
, and process.exit
is that process.abort
always exits Node.js immediately and generates a core file. In addition, no event callbacks will run.
Check out our hands-on, practical guide to learning Git, with best-practices, industry-accepted standards, and included cheat sheet. Stop Googling Git commands and actually learn it!
Note: The core file just mentioned is not created if ulimit -c
is set to 0
. See this tutorial for more details.
To see it in action, replace the process.kill
call in our batman.js
file with a call to process.abort
and run node batman.js
.
// batman.js
// ...
setTimeout((function() {
return process.abort();
}), 5000);
When you run the program with node batman.js
, the program prints out the lines as before, but exits after the timeout has run, printing out some information about the state of the program when it aborted.
$ node batman.js
Batman begins
I'm Batman!
I'm Batman!
I'm Batman!
I'm Batman!
Abort trap: 6
The REPL
Since the REPL (Read–eval–print loop) is a different type of execution environment from your typical Node.js script, these exit strategies deserve their own special sections. This is because the REPL is an interactive environment and does not just implicitly exit like a script would.
As we've alredy mentioned in some of the previous sections, some of the above strategies will work in the REPL as well. However, it's not typical to use them in this case, and you should instead use the following methods.
Exiting using a Key Combination
Unlike vim, exiting from the REPL is really simple. In a running REPL, you can exit it by using the key combination Ctrl + C
, and keying it in twice. This sends the SIGINT, or interruption signal, to the REPL. This is commonly used for quitting programs in POSIX systems.
Using the .exit Command
We can also exit a Node.js REPL by using the command ".exit". When you enter this in a running Node REPL, the current REPL will exit. This invocation works similar to the Ctrl + C
method discussed above.
Conclusion
In many cases, using process.exit
will suffice for exiting Node.js. However, as we saw, there are numerous alternatives. The various methods give you flexibility to exit from any particular place in your code. You need to be careful with this extra power, however, not to insert dangerous code that can result in unplanned exits, crashing your programs.