How to Create Node JS Reusable Modules

Filed Under: Node.js

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 README.md file as shown below;

    New-Node.js-Eclipse-Project

  • Create a Java Script file named “fabonacci.js”
    New-Node.js-File
  • Provide implementation to generate numbers in Fibonacci Sequence as below.

    fabonacci.js

    
    /**
     * JournalDEV  
     * Fabonacci Sequence : 1,1,2,3,5,8,13,...
     */
    function fabonacci(num1,num2){
    	return num1+num2;
    }
    
  • Export this module functionality to use in other Node JS Modules. Node JS Platform has provided “exports” object to do this as shown below;
    
    exports.fabonacci = fabonacci;
    

    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-JSON-File

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

    New-Node.js-package.json-File

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

    package.json

    
    {
      "name": "fabonacci",
      "version": "1.0.0",
      "description": "fabonacci sequence",
      "main": "fabonacci",
      "author": "JournalDEV",
      "engines":{
      	"node":"*"
      	}
    }
    

    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;

    Node.js-fibonacci-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

    
    {
      "name": "fabonacci-client",
      "version": "1.0.0",
      "description": "fabonacci sequence client",
      "main": "fabonacci-test",
      "author": "JournalDEV",
      "engines":{
      	"node":"*"
      	}
    }
    
  • Open command prompt and goto “fabonacci-client” folder and run command as shown in the image.
    NPM-install-command

    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.
    NPM-module-eclipse-project
  • Create a JavaScript file to test “fabonacci” module functions.

    fabonacci-client.js

    
    /**
     * JournalDEV  
     * Fabonacci Sequence : 1,1,2,3,5,8,13,...
     */
    /**
     * New node file
     */
    var fab = require("fabonacci");
    var num = fab.fabonacci(1,1);
    console.log(num);
    
  • Finally project looks like below image;

    Node.js-fibonacci-client-eclipse-project

  • 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.

    
    var num = fab.fabonacci(1,2);
    

    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.

Comments

  1. Paul John says:

    Hi Pankaj,
    I am huge fan of your blogs and i have been following the node series closely.

    Now, I have a question here –
    After creating the fabonacci – shouldnt we run npm pack so that fabonacci-1.0.0.tgz gets created.
    Am I missing something ?

    Thanks,

    Paul
    https://twitter.com/pjk_cochin

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages