VOOZH about

URL: https://www.geeksforgeeks.org/node-js/node-js-assert-fail-function/

⇱ Node.js assert.fail() Function - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Node.js assert.fail() Function

Last Updated : 7 Aug, 2020
The assert module provides a set of assertion functions for verifying invariants. The assert.fail() function throws an AssertionError with the provided the error message or with a default error message. Syntax:
assert.fail([message])
Parameters: This function accepts following parameters as mentioned above and described below:
  • 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:
  1. You can visit the link to Install assert module. You can install this package by using this command.
    npm install assert
    Note: Installation is an optional step as it is inbuilt Node.js module.
  2. After installing the assert module, you can check your assert version in command prompt using the command.
    npm version assert
  3. After that, you can just create a folder and add a file for example, index.js as shown below.
Example 1: Filename: index.js Steps to run the program:
  1. The project structure will look like this: 👁 Image
  2. Run index.js file using below command:
    node index.js
    Output:
    Error: AssertionError [ERR_ASSERTION]: Failed at Object.
https://nodejs.org/dist/latest-v12.x/docs/api/assert.html#assert_assert_fail_message
Comment

Explore