Understanding Node.js Modules

Dumbing down the basics of Node.js modules.

Understanding Node.js Modules

Modules in Node. Js are basically blocks of encapsulated and reusable code installed by the developer on their applications and and are invoked and used appropriately by the external application on the basis of their related functionality. they can be single or multiple JavaScript files which can be reused throughout the Node.js application. In this article, i will explore the uses of the common Node.Js modules and their installation.

Module Types
Node. js has three types of modules:

  1. Core modules.
  2. Local modules.
  3. Third-party modules.

Core modules.

These modules are the bare minimum modules and come with the installation of Node.js. These modules are compiled into its binary distribution and loaded automatically when Node.js server is started. Some of the most important core modules include:

  • http - provides and allows for an HTTP client/server implementation. This also lets you create a server.

  • url - url module includes methods for URL resolution and parsing.

  • fs(file system module) - provides an API for interacting with the file system.

  • path - provides utilities for working with file and directory paths.

  • os - provides operating system-related utility methods and properties.
  • repl (Read Eval Print Loop) - provides a Read-Eval-Print-Loop (REPL) implementation that is available both as a standalone program or includible in other applications.
  • worker - enables the use of threads that execute JavaScript in parallel.
  • crypto - provides cryptographic functionality for passwords and any other sensitive information.
  • events - provides an API for managing events.
  • process - provides information about, and control over, the current Node.js process.

Read more on these module here.[nodejs.org/api/errors.html]

Local modules.

Local modules are modules created locally in your Node.js application. These modules include different functionalities of your application in separate files and folders. Here is the fun part: you can package these modules and share it via the NPM (Node Package Manager) for the node community to use it. We can create a local module that has a function to check whether a number is a prime number or not.

// 6k+-1 optimisation
function is_prime (n) {
  if (n < 3) return n > 1;
  else if (n % 2 === 0 || n % 3 === 0) return false;
  else if (n < 25) return true;
  let i = 5;
  while (i * i <= n ) {
    if (n % i === 0 || n % (i + 2) === 0) return false;
    i += 6;
  }
  return true;
}

// here exports is an object to which you can attach properties or methods.
exports.numberPrime = function(){
    return is_prime(10,30,80);
}

The module.exports is a special object which is included in every JavaScript file in the Node.js application by default. The module is a variable that represents the current module, and exports is an object that will be exposed as a module. So, whatever you assign to module.exports will be exposed as a module.

Third-party modules.

A third party module is any code that has been written by a third party. This is neither you nor the Node.js developers. You can use them to add functionality to your code without having to write it yourself. In a way, when you share your local module for other Node.js developers to use, to them that is a third party module.

They include: express, gulp, lodash, socket.io, mongoose among others.

Usage of Node.js modules.

In order for you to use these modules in your Node.js application, you will need to import them to your application. In order to do this you will use the require statement.

Screenshot (29).png

The require() function in the above example returns an object because http module returns its functionality as an object. In order to use the properties and methods of the declared object, (in this case our already declared modules) we use the dot notation. i.e. In order to create a server using the http module, we write:

var server = http.createServer(function(req, res){

  //your code goes here

});