Using Modules in Node JS - Praveen Kumar Ronald James

Using Modules in Node JS

Using Modules in Node JS

To write manageable code, we won’t be putting all the contents of the whole Node JS application in a single file, which even though might work out, it’s not going to be feasible when there are many developers developing the application. That will be a nightmare to manage or refactor or extend the code. Simply put, it’s not maintainable code, which is why the “require” is created.

The best approach is to split the code into logical modules, so we have different modules for different set of codes, which have certain functionalities in our application. We can call them anytime, only whenever we need them. For example, let’s say, we have a utility module that counts the items in an array for us. We would be creating a new module for it and then we will be calling the module when we need to count something.

Different Module Types

Alright, now that we know what we are doing, let’s go ahead and create a module. Before creating a new module, we need to understand the different types of modules. There can be two types as of now:

  • Node Package Manager (NPM) module
  • Custom Module

The npm modules require publishing it to the npm package repository before we can use it.

We’ll come to this at a later point of time.

Creating a Custom Module

Steps to Create a Custom Module

Now, for creating a simple counting module for our application, let’s create a custom module now. Remember, a module is just essentially another separate JavaScript file with the same code. Let’s follow the below steps to create a module.

  1. Create a new module-name.js file in the application. In our case, it’s going to be count.js.
  2. Create a variable with the same name as the file name and assign it to a function. In our case, it’s going to be var count.
  3. Do some operation on the function and return the result.
  4. Expose the required functionalities by using module.exports variable.
  5. Use the require function to include the custom JavaScript into the application. You can use this function from wherever you are. This is one of the Global Functions in Node JS.
  6. Call the external function using the locally require d variable.

Creating the New Module

Now let’s start creating a new file called count.js and start writing our code for the module.

The first thing we need to do is to create a counting function. We’ll use the traditional method of creating a variable and assigning it to an anonymous function expression.

var count = function () {};

We have to return a string that says, “There are so many items in this list”. To do something like that, we need to use a return statement.

Also, this program takes an array as its only parameter. Let’s add the function parameter and the return values, so that the function is complete.

var count = function ( arr) {

// Get the length of the array.
var len = arr.length;
// Return the string that conveys the number of elements.
return”There are “+len+” items in this array.”;

Now that we have created a function, let’s try testing it out. To get this function running, we just need to call it using the count(array) syntax. For simplicity, I am going to use only an inline array.

count ([ “Praveen”, “Purush”, “Science”,”Kittens”]);

The above function will return the following string:

There are 4 items in this array.

So, let’s try this on a full application and run it. Since it returns a string, we need to log it to the console or somewhere to be able to see it. Let’s use our best friend, console.log() for the same.

var count = function (arr) {

// Get the length of the array.
var len = arr.length;
// Return the string that conveys the number of elements.
return “There are “+len+” items in this array.”;

To run the above function, let’s save the code as count.jsand then run it using our known command node count (we skip the .js part as Node JS is smart enough to look for JavaScript files in the same directory) and see what happens:


$ node count
There are 4items in this array.

Yay! That works. We have created our own little nifty function that counts the number of items in an array and it also gives us in a nice way. In our next week’s article, we’ll see how to include our newly created module in our app.js.

Thanks to Praveen Kumar for being our guest writer this week.

Book in for a free 10-minute candidate attraction call with us today? Please click here to make an appointment.

© Ronald James Ltd. 09824756. Q16 Business Exchange, Quorum Business Park, Newcastle upon Tyne, Tyne and Wear, NE12 8BX, United Kingdom. Website by Outlines Design.