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:
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:
This will create a new folder, now we need to head inside the folder using the following command:
Now that you are inside the folder that you created, you can start a new node project using the command:
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:
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:
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:
Create a new file in the same folder and name it as “index js”, and type in the following line inside of that file:
This line will include our module in our index.js file as well, we can now call the “getRandomCar()” function using the “cars” variable:
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:
`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:
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.