How to prevent your Node.js process from crashing

Alberto Gimeno
Feb 7, 2018 · 3 min read

Interviewing some people for Node.js job positions I found two questions that nobody was answering correctly, and that frightened me:

  • What happens if you don’t handle a rejected promise in Node.js? How can you debug that?
  • What happens if you don’t handle the ‘error’ event in a stream?

Think about it. Do you know the answers? Are you sure? If not, you can compare yourself with this guy:

Image for post
Image for post

What happens if you don’t handle a rejected promise in Node.js? How can you debug that?

In the latest versions of Node.js (8.x, 9.x and probably earlier versions too) you get a few warnings printed to the stderr like this:

There are two important things here:

  • The process doesn’t crash. The process continues to work. But you get noticed.
  • However, this behavior will change in the future! In future versions of Node.js the process will crash.

This is great to know. Now you might be wondering how you can react to unhandled rejections programmatically. For example you might be interested in sending the stack trace to a crash reporting service. No worries, Node.js has something for you. You can listen to the event emitted by

For promises rejected with an error (e.g. ) this will print both the message and the stack trace. In any other case this will print the object passed to reject the promise. So, it is important to always reject with an error object in order to get a stack trace!

You can also listen to the event emitted by and you will get information about unhandled rejections and much more.

What happens if you don’t handle the ‘error’ event in a stream?

Create an app with just the following code:

What happens? BOOM! 💥 Your process exits with a non-zero exit code and this is printed:

Now, imagine somebody is uploading a file to your website, you are handling it with streams because it can be a big file, the user forgets about it and closes his laptop. The stream closes unexpectedly and… your Node.js process crashes and all the users of your app get errors. You don’t want that, right? Then, do not forget to handle the event in all your streams!

Extra ball: JSON.parse()

This apparently harmless function will throw an error if the input string is not valid JSON. Always, always, always add a try-catch function for it or wrap it in a promise. The same applies to any method in the Node.js API. Otherwise something like this will kill your process:

Simple and nice tic-tac bomb ☝️ waiting to crash your app on a Sunday.

Image for post
Image for post

Remember:

  • Always handle promise rejections. Listen to unhandled rejection events for crash reporting. And always use errors to reject promises in order to get stack traces!
  • Always handle the event for streams!
  • Wrap and any function in a try-catch block or inside a Promise. Well, in general any function that can throw errors.

DailyJS

JavaScript news and opinion.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store