Talespin_Kit Talespin_Kit - 1 year ago 216
Node.js Question

How to use nodejs Assert

How to use the Assert in nodejs http://nodejs.org/api/assert.html



It throws following error

ReferenceError: Assert is not defined

How to use "assert". I am new to javascript and nodejs. The nodejs documentation explictly mentions some of the sections as a class Example:- http://nodejs.org/api/all.html#all_class_buffer_1. But Assert is not mentioned as a class. Then what is "Assert"?

Answer Source

This module is used for writing unit tests for your applications, you can access it with require('assert').

var assert = require('assert');

  function() {
    throw new Error("Wrong value");
  function(err) {
    if ( (err instanceof Error) && /value/.test(err) ) {
      return true;
  "unexpected error"
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download