Node js Modules

javascript Oct 14, 2019

Node js Modules are referenced either by file path or by name. A Node js Modules that is referenced by a name will eventually map into a file path unless the module is a core module. Node’s core modules expose some Node core functions to the programmer, and they are preloaded when a Node process starts.

Want to generate node api directly from your browser visit Smartcodehub

Other Node js Modules include third-party modules that you installed using NPM (Node Package Manager) or local modules that you or your colleagues have created.

Each module of any type exposes a public API that the programmer can use after the module is imported into the current script. To use a module of any type, you have to use the require function like this:

var module = require('module_name');

This will import a core module or a module installed by NPM. The require function returns an object that represents the JavaScript API exposed by the module. Depending on the module, that object can be any JavaScript value — a function, an object with some properties that can be functions, an array, or any other type of JavaScript object.

The CommonJS module system is the only way you can share objects or functions among files in Node. For a sufficiently complex application you should divide some of the classes, objects, or functions into reusable well-defined modules. To the module user, a module exposes exactly what you specify it to.

In Node, files and modules are in one-to-one correspondence, which you can see in the following example. Start by creating a file named circle.js, which just exports the Circle constructor:

function Circle(x, y, r) {
  
  function r_squared() {
    return Math.pow(r, 2);
  }
  
  function area() {
    return Math.PI * r_squared();
  }
  
  return {
    area: area
  };
}
 
module.exports = Circle;

The important bit here lies on the last line, where you define what is to be exported by the module. module is a variable that represents the module you are currently in. module.exports is the object that the module will export to other scripts that require this module. You can export any object. In this case, you are just exporting the Circle constructor function, which a module user can use to create fully functional Circle instances.

you can also export more complex objects. module.exports is initialized with an empty object, which you can populate with the attributes you want to export. For instance, you can devise a module that exports a set of functions:

function printA() {
  console.log('A');
}
 
function printB() {
  console.log('B');
}
 
function printC() {
  console.log('C');
}
 
module.exports.printA = printA;
module.exports.printB = printB;
module.exports.pi = Math.PI;

This module exports two functions (printA and printB) and a number (pi). A client of this module would look something like this:

var myModule2 = require('./myModule2');
myModule2.printA(); // -> A
myModule2.printB(); // -> B
console.log(myModule2.pi); // -> 3.141592653589793

Neeraj Dana

Experienced Software Engineer with a demonstrated history of working in the information technology and services industry. Skilled in Angular, React, React-Native, Vue js, Machine Learning