Node.js Error Handling

Node.js Error Handling – ReferenceError

Moving along through our detailed Node.js Error Handling series, in today’s article we’ll be checking out the ReferenceError in all its glory. If you’re familiar with plain JavaScript ReferenceErrors, the ReferenceError class in Node will look quite familiar, since it performs the same role: When a reference is made to an object that hasn’t been defined in previously executed statements, a ReferenceError is thrown.

Within this article we’ll explore the ReferenceError in greater detail, starting with where it sits in the overall Node.js Error Class Hierarchy. We’ll also look at some functional code samples to illustrate a common scenario in which a ReferenceError might be thrown in your own code, so let’s get to it!

The Technical Rundown

Most Node.js errors inherit from the Error base class, or extend from an inherited class therein. The full error hierarchy of this error is:

Full Code Sample

Below is the full code sample we’ll be using in this article. It can be copied and pasted if you’d like to play with the code yourself and see how everything works.

When Should You Use It?

As discussed in the introduction, a ReferenceError is a common programming fault that occurs when code references an object that hasn’t been previously defined or initialized. The most common cause of such an error is simple typos. However, many programming languages (and common integrated development environments) are capable of detecting when a reference error is made due to a typo, and will alert you before you even execute your code. However, Node.js (and JavaScript that it’s built upon) is not a strongly-typed language, so many IDEs may have trouble determining if a reference is actually incorrect, or if the code is proper and intentional. Consequently, even if your IDE warns you about an invalid reference, you’re still able to execute your code since there’s no pre-compiler double-checking the code, which can lead to a higher rate of ReferenceErrors in JavaScript-based applications than in some other stricter languages.

To illustrate we have a very simple code sample. For a more realistic example we’ll be using our Book class module, which we can use to define simple book objects with properties like title, author, pageCount, and so forth:

We have two test functions that make use of our Book class. We’ll start with the passingTest() function, which instantiates a new Book and then uses the logging.log() method to output the object to the console:

Executing this function produces the following output:

It’s worth noting that the publicationType property is making use of the enum module found on NPM, which allows us to implement an enumeration for this property. Consequently, the simple console.log() output of our book instance object shows the full publicationType enum object representation for the default value of PublicationType.Digital. However, if we use a different form of output, such as the Book.getTagline() method, we get the expected string value representation of the publicationType property:

Anyway, everything above works just as expected, so let’s now look at our slightly modified version of this function, failingTest():

The name of the function is a strong indicator that something is wrong here. As it happens, you may notice that we instantiate the same Book instance again, but this time our call to logging.log() passes an invalid reference to boo, rather than book. As mentioned, the most common cause of ReferenceErrors is typos, so this scenario isn’t too out of the ordinary. Executing this code produces the following output, showing that a ReferenceError was, indeed, thrown:

Airbrake’s robust error monitoring software provides real-time error monitoring and automatic error reporting for all your development projects. Airbrake’s state of the art web dashboard ensures you receive round-the-clock status updates on your application’s health and error rates. No matter what you’re working on, Airbrake easily integrates with all the most popular languages and frameworks. Plus, Airbrake makes it easy to customize error parameters, while giving you complete control of the active error filter system, so you only gather the errors that matter most.

Check out Airbrake’s error monitoring software today and see for yourself why so many of the world’s best engineering teams use Airbrake to revolutionize their exception handling practices!