Skip to content
Related Articles

Related Articles

Error Classes in Node.js

Improve Article
Save Article
  • Last Updated : 21 Aug, 2020
Improve Article
Save Article

Node.js inherits the JavaScript and system errors from the JavaScript <Error> class and it guarantees to provide the properties which are available on that class. The JavaScript exceptions that immediately throws errors using the throw mechanism that are handled by try…catch construct that is provided by the JavaScript.

Node.js handles errors that occur during the application running, supports multiple error mechanisms i.e. how all these errors are reported and handled depends on Error type and API style. Application code can trigger user-specified errors also. All those errors that are generated by the Node.js are either the instances or inherited from the Error class. In Node.js, it experiences many types of errors while running applications that are given below:

Class: AssertionError: AssertionErrors are Extended by the <errors.Error> Class. When it detects that an exceptional logic violation has occurred that should never occur, then these errors are triggered and the assert module raises all these errors. All those errors that are thrown by the assert module are instances of AssertionError class.

Example 1: Filename: index.js

// Node.js program to demonstrate 
// Assertion error in JavaScript
// Importing Assert module
const assert = require('assert');
console.log("Throws Assertion Error...");
// Comparing equality using assert
    {'Alfa':'hi', 'beta':'hello'}, 
    {'Alfa':'hi', 'beta':'hello'}
// Throws AssertionError

Run index.js file using the following command:

node index.js


Throws Assertion Error
>> Throws Assertion Error…
>> assert.js:101
 throw new AssertionError(obj);
AssertionError [ERR_ASSERTION]: Values have same structure but are not reference-equal:
{ Alfa: ‘hi’, beta: ‘hello’} at Object.<anonymous> (C:\Users\Ajay Kumar\Desktop\test2.js:12:8)…….  operator: ?[32m’strictEqual’?[39m}

Class: RangeError: It shows that the provided argument was not within the acceptable range of values. It could be a numeric range, or outside the set of options.

Example 2: Filename: index.js

// Node.js program to demonstrate 
// range error in JavaScript
// Importing http module
const http = require('http');
// Creating server with port no
// out of range
var server = http.createServer()
    .listen(46456656, (err, res)=>{
    // Throws Range Error


Throws Range Error
>> internal/validators.js:192
   throw new ERR_SOCKET_BAD_PORT(name, port);
RangeError [ERR_SOCKET_BAD_PORT]: options.port should be >= 0 and < 65536. Received 46456656……. code: ?[32m’ERR_SOCKET_BAD_PORT’?[39m}

Class: ReferenceError: It specifies that the variable anyone is trying to access is not defined. Such types of errors specify typos in code or a broken program. The instances of ReferenceError specify a bug in the code until an application is dynamically running code.

Example 3: Filename: index.js

// Node.js program to demonstrate 
// Reference error in JavaScript
try {
  const alfa = 10;
  const beta = alfa + gamma;
  // Throws with a ReferenceError 
  // because gamma is undefined
} catch (err) {
  // Handle the error here.


Throws Reference Error
>> gamma is not defined
>> ReferenceError: gamma is not defined
at Object.<anonymous> (C:\Users\Ajay Kumar\Desktop\test2.js:128:23)……at internal/main/run_main_module.js:17:47

Class: SyntaxError: It specifies that the program is not a valid JavaScript and it may be generated as a result of code evaluation. These errors mostly happen as a result of eval, Function, require, or vm.

Example 4: Filename: index.js

// Node.js program to demonstrate 
// Syntax error in JavaScript
try {
  // Import vm module
  require('vm').runInThisContext('alfa @ beta');
} catch (error) {
  // Prints a Syntax Error.


Throws Syntax Error
>> alfa @ beta
>> SyntaxError: Invalid or unexpected token
   at new Script (vm.js:99:7)……at internal/main/run_main_module.js:17:47

Class: SystemError: System errors that are generated by Node.js occur due to exceptions within its runtime environment. when an application violates an operating system constraint then these errors could be expected.

Example 5: Filename: index.js

// Node.js program to demonstrate 
// Reference error in JavaScript
// Importing fs module
const fs = require('fs');
// Callback function
function errorCallback(err, data) {
  if (err) {
    console.error('There was an error', err);
// Trying to read file that does not exist
fs.readFile('/some/non-existing/file', errorCallback);


Throws Error
>> There was an error [Error: ENOENT: no such file or directory, open ‘C:\some\non-existing\file’] {
 errno: -4058,
 code: ‘ENOENT’,
 syscall: ‘open’,
 path: ‘C:\\some\\non-existing\\file’}

Class: TypeError: It specifies that the argument provided is not an allowable type. For example, calling a function that actually doesn’t exist would be a TypeError. As if the form is of argument validation, it throws TypeError instances immediately.

Example 6: Filename: index.js

// Node.js program to demonstrate 
// Type error in JavaScript
try {
  if(1) {
    if(2) {
} catch (error) {

Run index.js file using the following command:

node index.js


Throws Type Error
>> TypeError: console.loo is not a function
   at Object.<anonymous> (C:\Users\Ajay Kumar\Desktop\test2.js:104:15)……at internal/main/run_main_module.js:17:47


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!