NodeJS Errors: List of 6 Types of NodeJS Errors

NodeJS Error Thumbnail

Node.js provides an error object which indicates different types of errors. A programmer can throw the error in the console to debug the program. It helps to indicate which type of error occurs like syntax, reference, and type errors. It also gives a description of that particular error.

An error object is more useful when we are creating a callback for functions, it is recommended to first check for an error, and if it occurs then throw it and stop the flow of execution of the program if no error occurs then execute the rest of the program.


functionName(arguments, callback(error){
	    throw error;
    } else {
        // callback body
} )

6 Types of Errors in NodeJS

Node.js provides a way to check the specific type of error and then perform different actions according to that. Let’s see them one by one.

1. Range Error

This error represents that the specified type of error is due to the particular value being more than the allowed range.


const arr = []

Here we are trying to increase the length of an array to outside its range.


Range Error

2. Reference Error

This error represents that the variable/object tried to access does not exist i.e wrong reference. 


const first = "hello";

Here we defined a constant first and set it to the string “hello”, but in the console, we mistakenly try to log the constant second which does not exist.


Reference Error

3. Syntax Error

This error represents that the syntax used is not supported in Node.js or is invalid.


constant first = "hello";

Here we are trying to initiate a constant by using the constant keyword rather than const, so Node.js is not able to understand it and returns a syntax error.


Syntax Error

4. Type Error

This error represents that the type used for a particular operation differs from the type required for that operation.


const num = 123;

Here we are trying to convert a number into uppercase by using the toUpperCase method which requires a string.


Type Error

5. URI Error

This error represents that the way global URI handling functions are implemented is not valid.



Here we are passing the “%” instead of a URI in the URI handling functions.


Uri Error

5. Internal Error

This error happens internally in the NodeJS engine when it is unable to handle the execution due to much data and the stack is crossed its maximum limit.

6. Custom Error

In Node.js custom error can be thrown by using the error constructor of the error class. The custom error passed during the creation of an error object from the error class.


const errorObj = new Error("Invalid"); = "Invalid URI";
errorObj.code = 404;
throw errorObj;


Custom Error 1


Error object used to throw the error. It shows the type of error that occurs which helps to detect the error specifically and debug the problem that causes the error. A program can define its own custom error using the Error class which contains an error constructor used to initial the value of the message, name, code, etc regarding the error. Hope this article helps you to understand the error in Node.js.