Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • NodeJS Tutorial
  • NodeJS Exercises
  • NodeJS Assert
  • NodeJS Buffer
  • NodeJS Console
  • NodeJS Crypto
  • NodeJS DNS
  • NodeJS File System
  • NodeJS Globals
  • NodeJS HTTP
  • NodeJS HTTP2
  • NodeJS OS
  • NodeJS Path
  • NodeJS Process
  • NodeJS Query String
  • NodeJS Stream
  • NodeJS String Decoder
  • NodeJS Timers
  • NodeJS URL
  • NodeJS Interview Questions
  • NodeJS Questions
  • Web Technology
Open In App
Next Article:
How to add a function in JSX ?
Next article icon

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

Last Updated : 20 Nov, 2021
Comments
Improve
Suggest changes
Like Article
Like
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


 



Next Article
How to add a function in JSX ?

D

devrajkumar1903
Improve
Article Tags :
  • Node.js
  • Web Technologies
  • NodeJS-Questions

Similar Reads

  • How to Override Functions of Module in Node.js ?
    Overriding functions in a Node.js module allows you to alter or extend the behaviour of existing functions without modifying the original module's code. This approach can be useful for customizing library functionality, adding new features, or fixing bugs in third-party modules. Here’s how you can a
    3 min read
  • How to add a function in JSX ?
    JSX is a syntax extension for JavaScript that allows us to write HTML-like code within our JavaScript files. It is commonly used in React applications to define the structure and layout of components. While JSX primarily focuses on rendering components and displaying data, it also provides the flexi
    2 min read
  • How to remove all Global Modules in Node.js ?
    Node.js is an open-source and cross-platform runtime environment for executing JavaScript code outside of a browser. You need to remember that Node.js is not a framework and it’s not a programming language. Most of the people are confused and understand it’s a framework or a programming language. We
    2 min read
  • How to read and write files in Node JS ?
    NodeJS provides built-in modules for reading and writing files, allowing developers to interact with the file system asynchronously. These modules, namely fs (File System), offer various methods for performing file operations such as reading from files, writing to files, and manipulating file metada
    2 min read
  • How to use EcmaScript Modules in Node.js ?
    Using ECMAScript Modules (ES Modules or ESM) in Node.js allows you to take advantage of modern JavaScript syntax for organizing and managing your code. ECMAScript Modules provide a more structured and standardized way to work with modules compared to CommonJS, which has been traditionally used in No
    2 min read
  • How to write code using module.exports in Node.js ?
    module is a discrete program, contained in a single file in Node.js. They are tied to files with one module per file. module.exports is an object that the current module returns when it is "required" in another program or module. We are going to see a simple code like the calculator to learn how to
    3 min read
  • How to use External Modules and NPM in a project ?
    Need for External Modules: For a large JavaScript application, it becomes difficult and messy to write the whole code in just one JavaScript file. This is where CommonJS comes into the picture and this CommonJS format defines a module format that can be used up for breaking your JS application into
    3 min read
  • How to Add Data in JSON File using Node.js ?
    JSON stands for Javascript Object Notation. It is one of the easiest ways to exchange information between applications and is generally used by websites/APIs to communicate. For getting started with Node.js, refer this article. Prerequisites:NPM NodeApproachTo add data in JSON file using the node js
    4 min read
  • How to Create and Use Functions in MySQL with NodeJS?
    We will learn how to create and use functions in MySQL with Node.js. MySQL functions allow encapsulating complex calculations and business logic within the database, which can then be called from Node.js applications. This method is particularly useful for reusing SQL code and maintaining a clean ap
    3 min read
  • How To Create Modules in NodeJS?
    Modules are the building blocks of NodeJS code. They help you break down your project into smaller, manageable pieces, each with its own job. To create a module, just make a JavaScript file and export what you want to share. Other files can then import and use those exports, adding that functionalit
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences