The Wayback Machine - https://web.archive.org/web/20240118181034/https://www.geeksforgeeks.org/node-js-assert-function/
Open In App
Related Articles

Node.js assert() Function

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

The assert module provides a set of assertion functions for verifying invariants. In assert() function, if the value is not truth, then a AssertionError is thrown with a message property set equal to the value of the message parameter. 

Syntax:

assert(value[, message])

Parameters: This function accepts the following parameters as mentioned above and described below:

  • value: This parameter holds the expression that need to be evaluated. It is of any type.
  • message: This parameter holds the error message of string or error type. It is an optional parameter.

Return Value: This function returns assertion error of object type. Installation of assert module:

npm install assert
  • Note: Installation is an optional step as it is inbuilt Node.js module.
  • After installing the assert module, you can check your assert version in command prompt using the command.
npm version assert
  • After that, you can just create a folder and add a file for example, index.js as shown below.

Example 1: Filename: index.js 

javascript




// Requiring the module
const assert = require('assert').strict;
 
// Function call
try {
    assert(0)
} catch(error) {
    console.log("Error:", error)
}


Steps to run the program:

  • The project structure will look like this:
  • Run index.js file using below command:
node index.js

Output:

Error: AssertionError [ERR_ASSERTION]: The expression evaluated to a falsy value: assert(0) at Object. (C:\Users\Lenovo\Downloads\index.js:6:5) at Module._compile (internal/modules/cjs/loader.js:1138:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:1158:10) at Module.load (internal/modules/cjs/loader.js:986:32) at Function.Module._load (internal/modules/cjs/loader.js:879:14) at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12) at internal/main/run_main_module.js:17:47 { generatedMessage: true, code: ‘ERR_ASSERTION’, actual: 0, expected: true, operator: ‘==’ }

Example 2: Filename: index.js 

javascript




// Requiring the module
const assert = require('assert').strict;
 
// Function call
try {
    assert(1)
    console.log("No Error Occurred")
} catch(error) {
    console.log("Error:", error)
}


Steps to run the program:

  • The project structure will look like this:
  • Run index.js file using below command:
node index.js

Output:

No Error Occurred

Reference: https://nodejs.org/dist/latest-v12.x/docs/api/assert.html#assert_assert_value_message


Learn to code easily with our course Coding for Everyone. This course is accessible and designed for everyone, even if you're new to coding. Start today and join millions on a journey to improve your skills!

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!

Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated : 16 Jun, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads
Complete Tutorials