How to add new functionalities to a module in Node.js ?
Modules are an integral part of nodeJS. One can require modules of three different types:
- In-built or default modules provided by NodeJS.
- Open-source modules that can be installed through npm or yarn.
- Private modules are defined by us programmers according to theirs need.
In this article, we are going to install the express module of NodeJS and add new functionalities to it. To add new functionality first import the module then adds functionalities according to our need.
<module_name>.<new_functionality_name> = expression or function
And then export the module. Let us walk through step by step to implement it.
Step 1: Create an “app.js” file in the project folder and initialize the project using npm.
Step 2: Create a “script.js” file and install the express package using npm.
npm install express
Step 3: Now let us code the “script.js” file. In it, we would require the express npm package, then add the new functionalities, and at last export the package. In it, we would be adding a variable, an object, and two functions for demonstration purposes.
Step 4: Now we will code the “app.js” file. In it, we would require the express module exported from the “script.js” file. And use that module to demonstrate the new and old functionalities of it.
Step 5: Run app.js file using below command: