How to Create Node JS Reusable Modules With Examples

In my previous post, we have discussed about “How to export and import a Node JS Module” with syntax and some examples. If you haven’t gone through my previous posts, please go through them first and come back to this point because now onwards we are going to use that knowledge to develop all our examples.

We have already discussed that Node JS is Modular Platform. Node JS Platform contains many modules or packages developed by Node JS or Third-Party clients. I guess you have some curiosity to know about the implementation of those existing Node JS Modules.

Node JS Platform has provided an approach to create our own modules (if required) to match our project requirements. It is very easy and simple to create our own Node JS Modules.

We are going to discuss the approach to create our own Node JS Modules and also how to reuse that module as a Node JS Library in other module in this post.

Real-time Scenario:
As Developer or even in academic, I guess we all have developed a simple program to print “Fibonacci sequence”.

Fibonacci sequence is a sequence of numbers in which first two numbers are 1, 1 and each subsequent number is the sum of the previous two numbers. For example 1,1,2,3,5,8,13 and so on.

To develop custom or our own Node JS Modules or Packages, we need to do the following two steps:

  1. Create new Node JS Module
  2. Publish our own Node JS Module

We will discuss these two steps in detail by using the scenario explained above.

How to create new Node JS Module

We are going to develop a new Node JS Module with “fabonacci” name. We are going to use Enide Studio IDE 2014 to develop this example.

Please use the following steps to create new Node JS Module:

  • Create a Node JS Project (Please refer “Node JS Basic Examples With Node REPL (CLI) and Node IDE” post for complete steps)Enter Project name: fabonacci

    Select Template to use: none/empty


  • New-Node.js-Project-
  • It creates a new Node JS Project with one file as shown below; 
  • New-Node.js-Eclipse-Project
  • Create a Java Script file named “fabonacci.js”
  • Provide implementation to generate numbers in Fibonacci Sequence as below.fabonacci.js

  • Export this module functionality to use in other Node JS Modules. Node JS Platform has provided “exports” object to do this as shown below;

    Please refer my previous post “Node JS: Export and Import Modules” for more details on exports object.

  • Create a package.json file at root directory as shown below. If you have already gone through my previous posts to create “simple” application, simply copy package.json from that project and modify accordingly. Otherwise, please go through the following two steps.Step 1: “New” >> “Other” to open “New Wizard” window
    Step 2: “JSON” >> “JSON File”
  • New-Node.js-Eclipse-Project


    Click on “Next” button and provide File name as package.json

  • New-Node.js-Eclipse-Project


    Click on Finish button. It creates empty JSON file. Copy below JSON content to package.json file and save it.


    If you don’t understand about this package.json file, please go through my previous post “Importance of package.json in Node JS Applications”.

  • Now our Node JS project looks like below; 
  • New-Node.js-Project-

How to use our Node JS Module

First we will create a new Node JS Project like “fabonacci-client” then we will use “fabonacci” module to test it.

  • Create new Node JS Project like “fabonacci-client”.
  • Node.js-fibonacci-client-project
  • Copy package.json file from “fabonacci” to “fabonacci-client” and modify accordingly.package.json
  • Open command prompt and goto “fabonacci-client” folder and run command as shown in the image.
  • NPM-install-command-450x111
    This step is known as publishing our own newly created Node JS Module into other Node JS Module.
  • Now “fabonacci-client” includes “fabonacci” module as shown below.
  • Node.js-execute
  • Create a JavaScript file to test “fabonacci” module functions.fabonacci-client.js
  • Finally project looks like below image; 
  • Node.js-execute
  • Run fabonacci-client.js in Enide Studio IDE 2014 as “Node Application”.
  • Node.js-execute

    Change fabonacci-client.js as shown below and run it again to see next number in the Fibonacci Sequence.


  • Node.js-execute-1

As a Java Developer, I guess you have already done this kind of activities in your projects. When we have a common or reusable component in our project requirements, then we will develop this component as a separate project, package this component into a JAR and place this JAR file in other Project module’s CLASSPATH, so that those dependent Project modules will reuse that component.

We did similar kind of approach in this post too. We have created a new Node JS Module – fabonacci, packaged that module, add this module into our new Node JS Project and reuse those functions defined in fabonacci in our new Node JS Project.

By admin

Leave a Reply