How to exit from a Node.js program
Learn how to terminate a Node.js app in the best possible way
There are various ways to terminate a Node.js application.
When running a program in the console you can close it with ctrl-C
, but what I want to discuss here is programmatically exiting.
Let’s start with the most drastic one, and see why you’re better off not using it.
The process
core module is provides a handy method that allows you to programmatically exit from a Node.js program: process.exit()
.
When Node.js runs this line, the process is immediately forced to terminate.
This means that any callback that’s pending, any network request still being sent, any filesystem access, or processes writing to stdout
or stderr
- all is going to be ungracefully terminated right away.
If this is fine for you, you can pass an integer that signals the operating system the exit code:
process.exit(1)
By default the exit code is 0
, which means success. Different exit codes have different meaning, which you might want to use in your own system to have the program communicate to other programs.
You can read more on exit codes at https://nodejs.org/api/process.html#process_exit_codes
You can also set the process.exitCode
property:
process.exitCode = 1
and when the program will later end, Node will return that exit code.
A program will gracefully exit when all the processing is done.
Many times with Node we start servers, like this HTTP server:
const express = require('express')
const app = express()
app.get('/', (req, res) => {
res.send('Hi!')
})
app.listen(3000, () => console.log('Server ready'))
This program is never going to end. If you call process.exit()
, any currently pending or running request is going to be aborted. This is not nice.
In this case you need to send the command a SIGTERM signal, and handle that with the process signal handler:
Note:
process
does not require a “require”, it’s automatically available.
const express = require('express')
const app = express()
app.get('/', (req, res) => {
res.send('Hi!')
})
const server = app.listen(3000, () => console.log('Server ready'))
process.on('SIGTERM', () => {
server.close(() => {
console.log('Process terminated')
})
})
What are signals? Signals are a POSIX intercommunication system: a notification sent to a process in order to notify it of an event that occurred.
SIGKILL
is the signals that tells a process to immediately terminate, and would ideally act like process.exit()
.
SIGTERM
is the signals that tells a process to gracefully terminate. It is the signal that’s sent from process managers like upstart
or supervisord
and many others.
You can send this signal from inside the program, in another function:
process.kill(process.pid, 'SIGTERM')
Or from another Node.js running program, or any other app running in your system that knows the PID of the process you want to terminate.
→ I wrote 17 books to help you become a better developer, download them all at $0 cost by joining my newsletter
→ JOIN MY CODING BOOTCAMP, an amazing cohort course that will be a huge step up in your coding career - covering React, Next.js - next edition February 2025