JavaScript

How to Create a Node.js Module

NodeJs modules are the same as vanilla JavaScript libraries, they are a block of code that is used by some external factor (some other javascript file or application). NodeJs is a javascript runtime environment, and just like vanilla javascript libraries, there are millions of NodeJs modules available on the internet. Modules, just like Js libraries, can be of a single file or can be cultivated from a bunch of files that work together to perform a specific task or to provide certain functionality.

Pre-Requisites: You need to have NodeJs environment installed and running on your machine to create a node module, for a guide on how to install NodeJS click here.

Including a module in your project

To use a particular module you need to use the keyword “require()” with the name of that module. if the module that you want to use is not a built-in module then you can download modules from the npm library by using the following command in the terminal:

npm installs <name-of-module>

Built-in Modules

NodeJS includes a lot of built-in modules, some examples of the built-in modules are:

  • timers
  • string_decoder
  • child_process
  • crypto
  • Https

Creating your own NodeJs Module

You can easily create your node modules and use other files of the same project or across different applications. To get started, let’s start by heading inside a folder and creating a new node project using npm. So, type in the following lines in the terminal of your code editor:

$ mkdir create_node_module

This will create a new folder, now we need to head inside the folder using the following command:

$ cd create_node_module

Now that you are inside the folder that you created, you can start a new node project using the command:

$ npm init

You will see the following result on your console:

Just follow with the terminal-screen guide and provide a package name, author name, etc.

In the end, it is going to ask you if you are okay with the package.json file:

Type yes or press enter.

Create a new file and name it “myfirstmodule.js”. In this file, we are going to store information about super-cars, so we need a data structure of Car which we can create with the following lines of code:

class Car {

constructor(name, model) {

this.name = name;

this.model = model;

}

}

Next we need a list of cars with each entry mapped on the data structure that we just created:

const allCars = [

new Car("McLaren", 2012),

new Car("Bugatti", 2020),

new Car("Ferrari", 2007),

new Car("Lambo", 2008),

new Car("Toyota", 1993),

new Car("Mustang", 2017),

];

Next up, we want a function that will randomly pick cars from our list. We will also need to export this function using the keyword “exports” so that any file that requires our module can have access to this function. Use the following lines of code:

exports.getRandomCar = () => {

return allCars[Math.floor(Math.random() * allCars.length)];

};

Create a new file in the same folder and name it as “index js”, and type in the following line inside of that file:

const cars = require("./myfirstmodule");

This line will include our module in our index.js file as well, we can now call the “getRandomCar()” function using the “cars” variable:

const randomCar = cars.getRandomCar();

The last thing that is left to do is to use the name and the model of the car that was randomly chosen. We do that by using the following command lines of code:

console.log(

`Today's hot pick is the ${randomCar.name} of the year ${randomCar.model}`

);

Now all we have to do is to run the index file by typing the following command in the terminal:

node index.js

You will see the following result on the console:

There you have it, you created your first node module, and used it in another file using exports and require statements. If you want to go the extra mile then you can publish this module on the npm library.

Conclusion

Node modules are the equivalent of JavaScript libraries, and that is why creating new node modules isn’t a tough job. To start, you need to set up a new project using “npm init” and then you can implement the node module in another file or application by using require and exports keywords. Currently, we only implement the usage of modules within a single machine, if you want, you can even publish your module on the npm library as well.

About the author

Shehroz Azam

A Javascript Developer & Linux enthusiast with 4 years of industrial experience and proven know-how to combine creative and usability viewpoints resulting in world-class web applications. I have experience working with Vue, React & Node.js & currently working on article writing and video creation.