What is the purpose of module.exports in node.js ?
Last Updated :
31 Mar, 2023
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() method.
Purpose:
- The main purpose of module.exports is to achieve modular programming. Modular programming refers to separating the functionality of a program into independent, interchangeable modules, such that each contains everything necessary to execute only one aspect of the desired functionality. By not using the module.exports it becomes difficult to write a large program without modular/reusable code.
- Using module.exports we can separate business logic from other modules. In other terms, we can achieve abstraction using it.
- By using it becomes easy to maintain and manage the code base in different modules.
- Enforces separation of concerns. Having our code split up into multiple files allows us to have appropriate file names for every file. This way we can easily identify what every module does and where to find it (assuming we made a logical directory structure which is still your responsibility.
Example: How to use module.exports in node.js. To start with the following example it is necessary to have node.js installed on your pc.
For verifying type the following command in the terminal. It will show the installed version of Node.Js on your pc.
node -v

Step 1: Create a separate folder and then navigate to that folder by terminal or command prompt.
Step 2:Â Run the npm init -y command in the terminal or command prompt to create a package.json file
Step 3:Â Now create two files at the root of your project structure named module.js and app.js respectively.Â
Project structure: It will look like this:

Step 4:Â Add the following code to the module.js file
JavaScript
// Module.js file
function addTwoNumbers(a, b) {
return a + b;
}
function multiplyTwoNumbers(a, b) {
return a * b;
}
const exportedObject = { addTwoNumbers, multiplyTwoNumbers };
// module.exports can be used to export
// single function but we are exporting
// object having two functions
module.exports = exportedObject;
Step 5:Â Add the following code to app.js file
JavaScript
// app.js file
const obj = require("./module");
// Getting object exported from module.js
console.log(obj);
// Printing object exported from
// module.js that contains
// references of two functions
const add = obj.addTwoNumbers;
// Reference to addTwoNumbers() function
console.log(add(3, 4));
const multiply = obj.multiplyTwoNumbers;
// Reference to multiplyTwoNumbers() function
console.log(multiply(3, 4));
Step to run the application:Â Run the following command in your terminal inside your root path of the project (eg:module_exports_tut) folder.
node app.js
Output: Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â

For more information about the module.exports visit  https://www.geeksforgeeks.org/node-js-export-module/
Similar Reads
What is the Purpose of Exports Array in NgModule? NgModule is a class marked by the @NgModule decorator. It identifies the module's own components, directives, and pipes, making some of them public, through the exports property, so that external components can use them. The @NgModule decorator takes a metadata object as an argument. This object con
3 min read
What is the purpose of the 'node_modules' folder ? The node_modules folder is a directory in NodeJS projects that stores third-party libraries and dependencies. It's essential for managing dependencies, which are packages or modules that a NodeJS project relies on. When you install a package using npm or Yarn, these tools download the package along
5 min read
What is the scope of require module in Node.js ? Whenever we have to use any feature of any other module shared by "module.exports" in our Node.js application, then we use require() method. That means we are importing those shared features into our own module and after that, we can use those features. Now the biggest question is -what is the scope
3 min read
What is the purpose of process object in Node.js ? A process object is a global object available in the Node.js environment. It is globally available. We do not have to use the require() to import the module of the process object. The "process" object use to get current Node.js process details & also give control over that process. Properties of
2 min read
What is the Purpose of __filename Variable in Node.js ? In Node.js, there are several global variables that are available in all modules. One such variable is __filename. This article delves into the purpose, usage, and practical applications of the __filename variable in Node.js.What is __filename?The __filename variable is a built-in global variable in
3 min read