Assert module in Node.js provides a bunch of facilities that are useful for the assertion of the function. The assert module provides a set of assertion functions for verifying invariants. If the condition is true it will output nothing else an assertion error is given by the console.
Install the assert module using the following command:
npm install assert
Note: Installation is an optional step as it is inbuilt Node.js module.
Importing module:
const assert = require("assert");
Example 1:
console.clear()
const assert = require( 'assert' );
let x = 4;
let y = 5;
try {
assert(x == y);
}
catch {
console.log(
`${x} is not equal to ${y}`);
}
|
Output:

Example 2:
console.clear()
const assert = require( 'assert' );
let x = 4;
let y = 5;
assert(x > y);
|
Note: In this example, no try-catch is given so an assertion error of the kind given below will be the output.
Output:

Note: Text Highlighted is the assertion error.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
07 Oct, 2021
Like Article
Save Article