Categories
JavaScript Answers

How to quit a Node.js app gracefully?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *