Node.js Export Module

12 Oct, 2021
The module.exports in Node.js is used to export any literal, function or object as a module. It is used to include JavaScript file into node.js applications. The module is similar to variable that is used to represent the current module and exports is an object that is exposed as a module.
Syntax:
module.exports = literal | function | objectExplanation: Here the assigned value (literal | function | object) is directly exposed as a module and can be used directly.
module.exports = literal | function | object
Explanation: Here the assigned value (literal | function | object) is directly exposed as a module and can be used directly.
module.exports.variable = literal | function | objectExplanation: Here the assigned value (literal | function | object) is indirectly exposed as a module and can be consumed using the variable.
module.exports.variable = literal | function | object
Explanation: Here the assigned value (literal | function | object) is indirectly exposed as a module and can be consumed using the variable.
Example 1: Exporting Literals
Create a file named as app.js and export the literal using module.exports.module.exports = "GeeksforGeeks";
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);
const company = require("./app");console.log(company);
Output:GeeksforGeeks
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'}
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);
const company = require('./app');  console.log(company.name);console.log(company.website);
Output:GeeksforGeeks
https://geeksforgeeks.org

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);}
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);
const sum = require('./app');  sum(2, 5);
Output:7
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.module.exports = function () {  this.name = 'GeeksforGeeks';  this.website = 'https://geeksforgeeks.org';  this.info = () => {    console.log(`Company name - ${this.name}`);    console.log(`Website - ${this.website}`);  }}
module.exports = function () {  this.name = 'GeeksforGeeks';  this.website = 'https://geeksforgeeks.org';  this.info = () => {    console.log(`Company name - ${this.name}`);    console.log(`Website - ${this.website}`);  }}
Create a file named index.js and import the file app.js to use the exported function as a class.const Company = require('./app');  const firstCompany = new Company();  firstCompany.info();
const Company = require('./app');  const firstCompany = new Company();  firstCompany.info();
Output:Company name - GeeksforGeeks
Website - https://geeksforgeeks.org

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

Example 5: Load Module from Separate Folders
const index = require('./models/lang/index.js');
Explanation: In the above example, the index.js file is under lang folder that is inside models folder. We can use the ./ to go to the root folder then we move relative to it.
Node.js-Basics
Picked
Node.js
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to update Node.js and NPM to next version ?
Node.js fs.readFileSync() Method
Node.js fs.writeFile() Method
How to update NPM ?
Difference between promise and async await in Node.js
Top 10 Projects For Beginners To Practice HTML and CSS Skills
Difference between var, let and const keywords in JavaScript
How to insert spaces/tabs in text using HTML/CSS?
How to fetch data from an API in ReactJS ?
Differences between Functional Components and Class Components in React