How to make a Node.js app not exit on error?

Sometimes, we want to make a Node.js app not exit on error.

In this article, we’ll look at how to make a Node.js app not exit on error.

How to make a Node.js app not exit on error?

To make a Node.js app not exit on error, we can add a listener for the uncaughtException with process.on.

For instance, we write

process.on('uncaughtException', (err) => {
  console.log('Caught exception: ', err);
});

to call process.on with 'uncaughtException' and a callback that runs when the uncaughtException event is emitted.

The uncaughtException event is triggered when there’s an unhandled error occurring in the app.

In the callback, we get the error object from err.

Conclusion

To make a Node.js app not exit on error, we can add a listener for the uncaughtException with process.on.