Built-in Modules

A module encapsulates relate code, such as code for handling files, into a single unit of code. Node ships with many useful modules. These are called built-in modules. Programmers can create new modules. These are called custom modules.

Following is a list of some built-in modules:

Module Description
assert used for unit testing
buffer for handling binary data
crypto for openssl
events for event handling
fs file handling
http make Node.js act as an HTTP server
https make Node.js act as an HTTPS server
net create clients and servers
os gives info about the os
path handling file paths
querystring handle url querystring
readline handle readable streams
stream handle streaming data
timers timing functions like counting milliseconds
tls for implementing TLS and SSL protocols
url parsing URL strings
util utility functions
zlib To compress or decompress files


Following is a simple example of testing an expression:

var assert = require('assert');
assert(1 > 10);

Since 1 not greater than 10, the assertion fails with the following error:

  throw new assert.AssertionError({
AssertionError: false == true
    at Object.<anonymous> (nodejs-ex/assert.js:3:1)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.runMain (module.js:604:10)
    at run (bootstrap_node.js:389:7)
    at startup (bootstrap_node.js:149:9)
    at bootstrap_node.js:502:3


The crypto module offers many functions for encrypting and decrypting strings, streams, and buffers. Following example encrypts and decrypts a string:

var cry = require('crypto'), algo = 'aes128', pass = 'UY675ygyf$$';

function encrypt(txt){
  var cipher = cry.createCipher(algo,pass)
  var enc = cipher.update(txt,'utf8','hex')
  enc += cipher.final('hex');
  return enc;

function decrypt(txt){
  var decipher = cry.createDecipher(algo,pass)
  var dec = decipher.update(txt,'hex','utf8')
  dec += decipher.final('utf8');
  return dec;

var mystring = encrypt("Encrypt me")


Encrypt me