How to quit a Node.js app gracefully?

Sometimes, we want to quit a Node.js app gracefully.

In this article, we’ll look at how to quit a Node.js app gracefully.

How to quit a Node.js app gracefully?

To quit a Node.js app gracefully, we can listen for the SIGINT event and call process.exit when it’s triggered.

For instance, we write

process.on('SIGINT', () => {
  process.exit();
})

to listen to the SIGINT event by calling process.on with 'SIGINT'.

In the event handler callback, we call process.exit without an an exit code to exit the app gracefully.

Conclusion

To quit a Node.js app gracefully, we can listen for the SIGINT event and call process.exit when it’s triggered.