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
  • DSA
  • Algorithms
  • Analysis of Algorithms
  • Sorting
  • Searching
  • Greedy
  • Recursion
  • Backtracking
  • Dynamic Programming
  • Divide and Conquer
  • Geometric Algorithms
  • Mathematical Algorithms
  • Pattern Searching
  • Bitwise Algorithms
  • Branch & Bound
  • Randomized Algorithms
Open In App
Next Article:
How to Import all Exports of a File as an Object in Node.js ?
Next article icon

Expose Functionality from a Node.js file using exports

Last Updated : 14 Oct, 2021
Comments
Improve
Suggest changes
Like Article
Like
Report

Module.exports API to expose Data to other files

Node supports built-in module system. Node.js can import functionality which are exposed by other Node.js files. To import something, you need to use the import functionality exposed by the library.js file that is present in the current file folder.

const library = require('./library')                // Path

The functionality in the file must be exposed before it could be imported into any other files. An object defined in the file by default is private and unexposed to the outer world.

The module.exports file API is offered by the module system to implement in the code.
module is a variable that represents the current module and exports is an object which will be exposed as a module. So, the module.exports and exports will both be exposed as a module.

module.exports is basically an object which returns the result of a require call.

You need the new exports property to import the object or function in any other parts of your app. You can do so in 2 ways:

The first way is to assign an object to module.exports where the object is provided out of the box by module system.

Examples:




const person = {
    firstName: 'John',
    lastName: 'Smith'
}
  
module.exports = Person
  
// in the file where you want to export
   
const person= require(‘./person)
 
 

The second way is by adding the exported object as a property of exports. You can use exports to export multiple objects, function or data:




const Person = {
    firstName: 'John',
    lastName: 'Smith'
}
  
  
exports.person = person
 
 

Or Directly




exports.person = {
    firstName: 'John',
    lastName: 'Smith'
}
 
 

You’ll use it by referencing a property of your import in the other file:




Const items = require('items')
items.person
 
 

Or




const person= require('./items').person
 
 

What’s the difference between module.exports and exports?

The first exposes the object it points to whereas the latter exposes the properties of the object it points to.



Next Article
How to Import all Exports of a File as an Object in Node.js ?

M

mayank021
Improve
Article Tags :
  • Algorithms
  • DSA
Practice Tags :
  • Algorithms

Similar Reads

  • How to export multiple values or elements in a Module ?
    In Node.js, modules are an essential part of the architecture, allowing you to organize code into separate files and reuse it across different parts of your application. One common requirement is to export multiple values or elements from a module so that they can be imported and used in other parts
    4 min read
  • How to Import all Exports of a File as an Object in Node.js ?
    In Node.js, you can import all exports of a file as an object using the ES Modules syntax (import) or the CommonJS syntax (require). This approach is useful when you have multiple exports from a module and want to access them conveniently through a single object interface. Below are the approaches t
    4 min read
  • How to work with Node.js and JSON file ?
    Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine. With node.js as backend, the developer can maintain a single codebase for an entire application in javaScript.JSON on the other hand, stands for JavaScript Object Notation. It is a lightweight format for storing and exchanging d
    4 min read
  • How to share code between Node.js and the browser?
    In this article, we will explore how to write JavaScript modules that can be used by both the client-side and the server-side applications.We have a small web application with a JavaScript client (running in the browser) and a Node.js server communicating with it. And we have a function getFrequency
    4 min read
  • Node Export Module
    In NodeJS, module.exports is used to share functions, objects, or values from one file to the other file so that other files can use them. This is an essential part of organizing and reusing code across different parts of your application, making it easier to manage and maintain. Here’s how exportin
    5 min read
  • How to make .js variables accessible to .ejs files ?
    EJS is a simple templating language that lets you generate HTML markup with plain JavaScript. It is possible to access JS variable in .ejs file. You just need to pass the JS object as second parameter of res.render() method. Let's jump into deep. Project Structure: Final folder structure will be as
    3 min read
  • What is the purpose of module.exports in node.js ?
    The module.exports is actually a property of the module object in node.js. module. Exports is the object that is returned to the require() call. By module.exports, we can export functions, objects, and their references from one file and can use them in other files by importing them by require() meth
    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 include Functions from other files in Node.js ?
    Code reusability is an important pillar in modern day programming. Code Reuse means the practice of using an existing code for a new function or software. In this article, we would learn how to use functions from other files in Node.js. This functionality can be easily implemented using the inbuilt
    2 min read
  • How To Use An ES6 Import In Node.js?
    To use an ES6 import in Node.js, you need to make some configurations to support ES6 modules. While Node.js was originally built around the CommonJS module system (require() and module.exports), modern JavaScript development prefers using the ES6 module syntax (import and export). Node.js now suppor
    4 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