In Nodejs, Modules are a small encapsulated unit, which we can reuse, share with anyone, and it’s easier to maintain. It can be a single file or a collection of multiple files or folders.
Or we can say a set of a function that we want to include in our application.
Types of Module in NodeJS:
There are three types of Modules:
- Local Modules
- Core Modules Or Inbuilt Modules
- Third-Party modules
In Nodejs, if we want to add a set of functionality in our application, we can create it locally and use them, this locally created functionality is known as local modules. With this, we can divide a large program into small chunks. Let’s see how to create your own modules.
How to create own modules in Node
In the above example, we have created 4 simple functions to perform 4 basic mathematical operations. With the help of the module.exports keyword we have exported all functions created in file calci.js and now we can use these functions anywhere in our project by requiring the calci.js file.
In the app.js file, we have required the functionality of the calci.js file and now if we run the file by nodeapp.js command, As a result, we will get:
Similarly, we can divide a large & complex program into multiple small parts and can create modules for those.
Node js has a number of in-Built modules which we do not need installation. In order to use inbuilt modules we only need to import them using require() function. Some inbuilt modules are HTTP, fs, path, os, URL, util, etc.
We have already learned about the HTTP module. In this blog, we will learn about the file system module.
The file system Module helps us access the different files and directories from our computer.
When we work on a real-world project, we need to read any specific file, or maybe we want to save some data into a specific file. We can perform these operations using a file system module. To include this module we use the below command:
const fs = require(‘fs’);
Let’s understand a few common uses of the FS module.
Syntax: fs.readFile(fileName [,options], callback)
To read the file asynchronously, we use the fs.readFile() method. This method requires 3 parameters. The first parameter, we need to define the name or the path of the file. The second parameter, which is optional defines the type of the file or encoding to be specific and the third parameter is a callback function.
Creating & Updating Files:
To create a file asynchronously and save the data into the file we have below two methods.
Syntax: fs.writeFile(filename, data[, options], callback) Syntax: fs.appendFile(filename, data[, options], callback)
These methods accept four-parameter. The second parameter, we provide the data which we want to save into the file. Rest all three, are the same as we provide for the reading the file.
Now let’s understand the difference between these two methods. Assume a scenario where we have an existing file and we want to update the data into it. In the case of the writeFile() method, it will remove the existing data from the file and save the new data as new records. But in the case of appendFile() method, it will update the data at end of the existing data of the file.
Syntax: fs.unlink(filename, callback)
To delete the file asynchronously, we have the fs.delete() method.
Third-party modules are the modules that are available online using NPM (Node Package Manager). These modules can be installed in the project folder or globally. Some of the popular third-party modules are:- Mongoose, express, Axios, and many more.
Installation & Uninstall
npm install -g express // to install globally npm install --save express // to install locally npm uninstall express // to uninstall
In this article, we have learned, what are node modules, types of modules, how to create own modules & file system modules. Thank you for reading and if you really like the blog hit the like button, and to learn more, visit here.