Node Export Module - GeeksforGeeks (2024)

Last Updated : 18 Jan, 2024

Improve

In Node, the `module.exports` is utilized to expose literals, functions, or objects as modules. This mechanism enables the inclusion of JavaScript files within Node.js applications. The `module` serves as a reference to the current module, and `exports` is an object that is made accessible as the module’s public interface.

Syntax:

module.exports = literal | function | object

Note: Here the assigned value (literal | function | object) is directly exposed as a module and can be used directly.

Syntax:

module.exports.variable = literal | function | object

Note: The assigned value (literal | function | object) is indirectly exposed as a module and can be consumed using the variable.

Below are different ways to export modules:

Table of Content

  • Exporting Literals
  • Exporting Object:
  • Exporting Function
  • Exporting function as a class

Example 1: Exporting Literals

Create a file named as app.js and export the literal using module.exports.

module.exports = "GeeksforGeeks"; 

Create a file named as index.js and import the file app.js to print the exported literal to the console.

const company = require("./app"); 
console.log(company);

Output:

GeeksforGeeks

Example 2: Exporting Object:

Create a file named as app.js and export the object using module.exports.

module.exports = { 
name: 'GeeksforGeeks',
website: 'https://geeksforgeeks.org'
}

Create a file named as index.js and import the file app.js to print the exported object data to the console.

const company = require('./app'); 
console.log(company.name);
console.log(company.website);

Output:

GeeksforGeeks
https://geeksforgeeks.org

Example 3: Exporting Function

Create a file named as app.js and export the function using module.exports.

module.exports = function (a, b) { 
console.log(a + b);
}

Create a file named as index.js and import the file app.js to use the exported function.

const sum = require('./app'); 
sum(2, 5);

Output:

7

Example 4: Exporting function as a class

Create a file named as app.js. Define a function using this keyword and export the function using module.exports and Create a file named index.js and import the file app.js to use the exported function as a class.

Javascript

const Company = require('./app');

const firstCompany = new Company();

firstCompany.info();

Javascript

module.exports = function () {

this.name = 'GeeksforGeeks';

this.website = 'https://geeksforgeeks.org';

this.info = () => {

console.log(`Company name - ${this.name}`);

console.log(`Website - ${this.website}`);

}

}

Output:

Company name - GeeksforGeeks
Website - https://geeksforgeeks.org


Like Article

Suggest improvement

Share your thoughts in the comments

Please Login to comment...

Node Export Module - GeeksforGeeks (2024)

FAQs

Node Export Module - GeeksforGeeks? ›

In Node, the `module. exports` is utilized to expose literals, functions, or objects as modules. This mechanism enables the inclusion of JavaScript files within Node. js applications.

How do I export an entire module in node JS? ›

Use the full path of a module file where you have exported it using module. exports . For example, if the log module in the log. js is stored under the utility folder under the root folder of your application, then import it, as shown below.

Why we use module export 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() method.

What is the difference between export and export module? ›

We've seen that module. exports is used to export a single function or object from a module, while exports is used to export multiple properties or functions. It's important to understand the difference between these two methods so you can choose the right one for your module.

What is the function of module in node JS? ›

In Node. js, Modules are the blocks of encapsulated code that communicate with an external application on the basis of their related functionality. Modules can be a single file or a collection of multiple files/folders.

Top Articles
Coconut Oil for Dog Constipation
Lot Sizing
It’s Time to Answer Your Questions About Super Bowl LVII (Published 2023)
Design215 Word Pattern Finder
Goodbye Horses: The Many Lives of Q Lazzarus
Usborne Links
Pitt Authorized User
Apply A Mudpack Crossword
Back to basics: Understanding the carburetor and fixing it yourself - Hagerty Media
Joe Gorga Zodiac Sign
4156303136
W303 Tarkov
Flower Mound Clavicle Trauma
Craigslist Blackshear Ga
Samantha Lyne Wikipedia
Apply for a credit card
Google Doodle Baseball 76
Www Craigslist Com Bakersfield
Heart Ring Worth Aj
Bella Bodhi [Model] - Bio, Height, Body Stats, Family, Career and Net Worth 
Routing Number For Radiant Credit Union
Craigslist Battle Ground Washington
Target Minute Clinic Hours
Directions To Nearest T Mobile Store
Rogue Lineage Uber Titles
Elite Dangerous How To Scan Nav Beacon
Amelia Chase Bank Murder
Unable to receive sms verification codes
Marokko houdt honderden mensen tegen die illegaal grens met Spaanse stad Ceuta wilden oversteken
R/Airforcerecruits
HP PARTSURFER - spare part search portal
Restored Republic
Mg Char Grill
Palmadise Rv Lot
Golden Tickets
Www Craigslist Com Shreveport Louisiana
Sun Haven Pufferfish
Chuze Fitness La Verne Reviews
Frommer's Philadelphia & the Amish Country (2007) (Frommer's Complete) - PDF Free Download
Indiana Jones 5 Showtimes Near Cinemark Stroud Mall And Xd
About My Father Showtimes Near Amc Rockford 16
Bekah Birdsall Measurements
M&T Bank
Southwest Airlines Departures Atlanta
3500 Orchard Place
Tyco Forums
House For Sale On Trulia
Dietary Extras Given Crossword Clue
Guy Ritchie's The Covenant Showtimes Near Look Cinemas Redlands
Vcuapi
Craigslist Yard Sales In Murrells Inlet
Latest Posts
Article information

Author: Allyn Kozey

Last Updated:

Views: 6138

Rating: 4.2 / 5 (63 voted)

Reviews: 86% of readers found this page helpful

Author information

Name: Allyn Kozey

Birthday: 1993-12-21

Address: Suite 454 40343 Larson Union, Port Melia, TX 16164

Phone: +2456904400762

Job: Investor Administrator

Hobby: Sketching, Puzzles, Pet, Mountaineering, Skydiving, Dowsing, Sports

Introduction: My name is Allyn Kozey, I am a outstanding, colorful, adventurous, encouraging, zealous, tender, helpful person who loves writing and wants to share my knowledge and understanding with you.