Open In App

How to add new functionalities to a module in Node.js ?

Improve
Improve
Like Article
Like
Save
Share
Report

Node.js is an open-source and cross-platform runtime environment built on Chrome’s V8 JavaScript engine for executing JavaScript code outside of a browser. You need to recollect that NodeJS isn’t a framework, and it’s not a programming language. In this article, we will discuss how to add new functionality to a module.

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.

Syntax:

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

npm init

Step 2: Create a “script.js” file and install the express package using npm.

npm install express

Project structure:

Project Structure

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.

Filename: script.js

Javascript




// Requiring the express module installed through npm
const express = require('express')
 
// Added a variable
express.fact = 'GeeksforGeeks is very informative'
 
// Added an object
express.info = {
    month: 'October',
    year: '2021'
}
 
// Added a function
express.print = function(str){
    return 'Your given parameter was : '+str
}
 
// Added a function
express.add = function(a,b){
    return a+b
}
 
// Exported so that modified express
// module can be used
module.exports = express


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.

Filename: app.js

Javascript




// Requiring modified express module
// from script.js
const express = require('./script.js')
 
// The default attribute of express
// that makes the app object
const app = express()
 
// New functionality of variable
console.log(express.fact)
 
// New functionality of object
console.log(express.info)
 
// New functionality of function
console.log(express.print('NodeJs'))
 
// New functionality of function
console.log(express.add(2,3))
 
// Default functionality to create server
app.listen(3000,function(req,res){
    console.log('Server started at port 3000')
})


Step 5: Run app.js file using below command:

node app.js

Output:

output

 



Last Updated : 20 Nov, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads