Modules

Modules are used to bring external functionality into your application.

var module1 = require('module1');

In the above statement, the require function loads a module and assigns it to a variable.

A module can export variables, functions and objects.

You can load as little functionality as you want from module as below:

var function1 = require('module1').function1;
console.log(function1());

Sources of Modules

3 types of modules in Node.js
Built-in Modules
var os = require('os');
console.log(os.hostname());
console.log(os.freemem());
console.log(os.totalmem());

Above ‘os’ module is a built-in module of Node.js

Custom Modules:
// File Name: math.js
const add = function(num1, num2) {
   console.log(num1 + num2);
}

module.exports = add;
  • Writing a module is a great way to modularize your code. It will become easy to develop and maintain the application.
  • A variable or a function can be exported using module.exports
  • Variables and functions that are not exported will be inaccessible outside of this JavaScript file.
// File name: app.js
var math = require('./math');
math.add(2, 5);
  • The module file can be loaded using require method having path of the module
  • While loading the module, you can omit “.js”

Exporting multiple functions or variables:

  • When you export using “module.exports” it export only 1 functioin/variable.
  • When you want to export multiple functions/variables, you can write exports.function_name as below
// File Name: math.js
exports.add = function(num1, num2) {
   console.log(num1 + num2);
}

exports.subtract = function(num1, num2) {
   console.log(num1 - num2);
}
// File name: app.js
var math = require('./math');
math.add(2, 5);
math.subtract(2, 5);
Third-Party module
  • Node Package Manager (npm) registry contains many third party node modules that you can download and use in your application
  • Modules can be installed from npm registry using below command
npm install module_name
  • Above command will download the module from npm registry into node_modules folder of your application.
my_app > node_module > module_name
  • Once the module is installed, you can load it the same way you load a built-in module
const request = require('request');

request('https://techstackjournal.com/node-js/', function(error, response, body) {
    if(response.statusCode === 200) {
        console.log(body);
    }
});
  • Some of the module provide command line utilities like gulp. They can be installed as below
npm install -g module_name
  • This will install the module and the executables of this module are placed in PATH, so that they are available globally in your machine.

Various ways of requiring a module

const functionName = require('./module_name');

Looks for module_name.js in the current directory and loads it.

const functionName = require('../module_name');

Looks for module_name.js in the parent directory and loads it.

const functionName = require('module_name');
  • Looks for module_name.js in the current directory and loads it.

Installing modules locally vs globally

Installing module locallyInstalling module globally
Commandnpm install module_namenpm install module_name -g
LocationApp > node_modules > module_namenpm_home > node_modules > module_name
requireYou will be able to load the module using require in your applicationCannot load the module using require in your application
Command-line utilityCommand-line utilities are accessibleCommand-line utilities are accessible
Difference between module local installation vs global installation