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:
- Create new Node JS Module
- 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: fabonacciSelect Template to use: none/empty
- It creates a new Node JS Project with one README.md file as shown below;
- Create a Java Script file named “fabonacci.js”
- Provide implementation to generate numbers in Fibonacci Sequence as below.
fabonacci.js
123456789/*** 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;
123exports.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” - Click on “Next” button and provide File name as package.json
- Click on Finish button. It creates empty JSON file. Copy below JSON content to package.json file and save it.
package.json
123456789101112{"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;
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”.
- Copy package.json file from “fabonacci” to “fabonacci-client” and modify accordingly.
package.json
123456789101112{"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.
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.
- Create a JavaScript file to test “fabonacci” module functions.
fabonacci-client.js
123456789101112/*** 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;
- Run fabonacci-client.js in Enide Studio IDE 2014 as “Node Application”.
- Change fabonacci-client.js as shown below and run it again to see next number in the Fibonacci Sequence.
123var num = fab.fabonacci(1,2);
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.