Export a Node JS Module
First we need to understand is why we need to export a Node JS Module?
Node JS has provided almost all required modules (We can check this updates on its official website: https://www.npmjs.com/. It is also known as Node JS Module Repository). But in some realtime applications, we may have some application functionality, which is used many places in that application but not available at Node JS Module Repository.
In this scenario, to get Reusability benefit, we need to create our own new Node JS module. Just creating new Module is not enough to use it in other modules of the system. We need to export this module so that other modules will reuse it.
Node JS Platform has provided a technique to export the following things so that other modules can reuse them without redefining.
- Variable
- Function
- Module
To export all these three, we have to use same technique. Node JS has provided an Object “exports” to do this.
Syntax :
exports.[variable/function/module]name = [variable/function/module]name
How to export a JavaScript Variable
We use Node JS “exports” object to export a Node JS Module’s Variable so that other Modules can reuse it.
Syntax :
exports.variable-name = variable-name
Example :
Let us assume that we have created a simple Node JS Project with one JavaScript file: myval.js file with the following content.
var exVal = 3.1416
exports.exVal = exVal;
Here we have exported exVal variable as exports.exVal with name exVal. That’s means other Node JS project can use this variable very easily just using PI name.
How to export a JavaScript Function:
We use Node JS same “exports” object even to export a Node JS Module’s JavaScript function so that other Modules can reuse it. In realtime, we may do this but it is not recommended to use always.
Syntax :
exports.function-name = function-name
Example :
Let us assume that we have created a simple Node JS Project with one JavaScript file: arthmetic.js file with the following content.
function addition(a,b){
return a + b;
}
function subtraction(a,b){
return a - b;
}
function multiplication(a,b){
return a * b;
}
function division(a,b){
return a / b;
}
exports.addition = addition
exports.subtraction = subtraction
exports.multiplication = multiplication
exports.division = division
Here we have exported all 4 JavaScript functions separately. That’s means other Node JS project can reuse them directly.
How to export a Node JS Module:
We use Node JS same “exports” object even to export a Node JS Module so that other Modules can reuse it. In realtime, it is recommended.
Syntax :
exports.module-name = module-name
Example :
exports.arthmetic = {
var exVal= 3.1416;
function addition(a,b){
return a + b;
}
function subtraction(a,b){
return a - b;
}
function multiplication(a,b){
return a * b;
}
function division(a,b){
return a / b;
}
}
Here we have exported all 4 JavaScript functions and ‘exVal’ variable with just one exports statement. That’s means other Node JS project can reuse all functions and ‘exVal’ very easily.
Import a Node JS Module
If we observe a Node JS Application or Module, it may dependent on other existing Node JS modules or our own Modules.
The major advantage of Modularity is reusability. We don’t need to redevelop the same existing functionality. We can import a Module and reuse that module functionality very easily.
How to import a Node JS Module:
Node JS Platform has provided a function call “require()” to import one module into another.
Syntax :
var some-name = require('module-name')
Here module-name is our required Node JS module name.
some-name is reference name to that module.
This require() call import the specified module and cached into the application so that we don’t need to import it again and again.
Example :
To import our own Node JS module
var arthmetic = require("arthmetic");
To import existing Node JS Module
Import Node JS “express” module;
var arthmetic = require("express");
Import Node JS “mongoose” module;
var mongoose = require("mongoose");