Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
  • Discussion on custom modules in nodejs

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 259
    Comment on it

    Welcome to FindNerd. Today we are going to discuss custom modules in Nodejs. As we have discussed the in-build modules as well as other modules which are provided by npm. We can also create our own modules. Kindly use the following steps.


    Step1: Create a folder with any name for your application files. Here we are creating folder named node_learning


    Step2 : Create a folder  for your module with any name. Here we are creating folder named train.


    Step3 : Create package.json file inside train folder and set the dependencies. You can use the below command for file creation.

    npm init

    Kindly check the screen-shot below.




      "name": "train",
      "version": "0.0.0",
      "description": "A module for keeping track of a train",
      "main": "index.js",
      "scripts": {
        "test": "echo \"Error: no test specified\" && exit 1"
      "author": "Deepak <>",
      "license": "BSD-2-Clause"


    With this file other users can install the dependencies for your module if they are using your module in their applications.


    Step 4 : Create index.js file and write the code for your module. Here we are dealing with train informations such as number origin,destination, arrival, departs, actualArrival, actualDeparts. Kindly check the code below.


    // index.js
    var Train = function(){
     = {
         	number: null,
            origin: null,
            destination: null,
        this.fill = function(info){
        for(var prop in{
        	if([prop] !== 'undefined'){[prop] = info[prop];
         this.triggerDepart = function(){
         this.triggerArrival = function(){
         this.getInformation = function()
    module.exports = Train;


    In above code we created a function Train and created property or object named data and other functions such as fill, triggerDepart, triggerArrival, getInformation. In the end pass the function to module.exports.

    We have created a simple module for train information. Now we will load this module in our application.


    Step 5: Kindly check below code.

    var train = require('./train');
    var train_info = {
    var tn = new train();


    In above code we have loaded the module train using require function and created a object with train information. We call the functions such as fill function to set the train information, triggerDepart to set the actual departure time, triggerArrival function to set the actual arrival of the train, getInformation function to display the flight data on console.


    Step 6: You can run the program by using below command.

    // go to application folder
    node main.js


    These are the basic steps to create and load the custom module.

    Thank you for being with us!

 0 Comment(s)

Sign In

Sign up using

Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: