Tags

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.”;

};

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

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:

praveen@PRAVEEN.SCIENCE MINGW64 ~/NodeJS

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

Who Are Ronald James?

We are a leading niche digital & tech recruitment specialist for the North East of England. We Specialise in the acquisition of high-performing technology talent across a variety of IT sectors including Digital & Technology Software Development.

Our ultimate goal is to make a positive impact on every client and candidate we serve - from the initial call and introduction, right up to the final delivery, we want our clients and candidates to feel they have had a beneficial and productive experience.

Contact our Team

If you’re looking to start your journey in sourcing talent or find your dream job, you’ll need a passionate, motivated team of experts to guide you. Check out our Jobs page for open vacancies. If interested, contact us or call 0191 620 0123 for a quick chat with our team.

Let's be Friends!

Follow us on our blog, FacebookLinkedInTwitter or Instagram to follow industry news, events, success stories and new blogs releases.


 

Back to Blog

</Follow Us>