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 exporting modules in NodeJS can help:
- Share code between files easily.
- Organize projects into smaller files.
- Reuse code without repetition.
- Control access to file contents.
Syntax
module.exports = literal | function | object
In this syntax:
- module. exports a value from a file for use in other files.
- You can export a literal, function, or object using this syntax.
- Other files can access the exported value via the require() function.
Note: Here the assigned value (literal | function | object) is directly exposed as a module and can be used directly.
Using the module.exports
In NodeJS, you can share code from one file so that it can be used in another file. This is done by using module.exports
to export the code.
1. Exporting a Function
In NodeJS, you can export a function from one module (file) so that it can be used in another module. This is done using module.exports. By exporting a function, you allow other files to import and use that function in their code.
Now, let us understand with the help of the example:
//math.js (module file)
// Define a function that adds two numbers
function add(a, b) {
return a + b;
}
// Export the function using module.exports
module.exports = add;
//app.js(main file)
// Import the add function from the math.js module
const add = require('./math');
// Use the imported function
console.log(add(2, 3)); // Output: 5
In the example:
- In this case, we are exporting the add function using module.exports, so it can be accessed from other files.
- require('./math') imports the function that was exported in math.js, and then it’s used in app.js.
2. Exporting function as a class
Create a file named app.js. Define a function using this keyword and export the function. module.exports and
Create a file named index.js and import the file app.js to use the exported function as a class.
Now, let us understand with the help of the example:
const Company = require('./app');
const firstCompany = new Company();
firstCompany.info();
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
3. Exporting Literals
Exporting Literals in NodeJS means sharing simple values like strings, numbers, or arrays from one file to app.js and exporting the literal using. module.exports
.
Now, let us understand with the help of the example:
module.exports = "GeeksforGeeks";
const message = require("./app");
console.log(message);
Output
GeeksforGeeks
In this example:
- In app.js, module.exports = "GeeksforGeeks"; exports the string "GeeksforGeeks".
- In index.js, const message = require("./app"); imports the exported string from app.js, and console.log(message); prints it to the console.
4. Exporting Object
It allows sharing multiple related values from one file to another. An object can store properties and methods, which can be accessed in other files.
Now, let us understand with the help of the example:
module.exports = {
name: "GeeksforGeeks",
website: "https://geeksforgeeks.org"
};
const company = require("./app");
console.log(company.name);
console.log(company.website);
Output
GeeksforGeeks
https://geeksforgeeks.org
In this example:
- app.js exports an object containing two properties: name and website.
- index.js imports the object from app.js and logs the property names and website to the console.
- The output shows the values of these properties: "GeeksforGeeks" and "https://geeksforgeeks.org".
exports vs module.exports
exports | module.exports |
---|---|
Used to export individual properties or functions | Used to export an entire module (object, function, or literal) |
Add properties or functions to the | Directly assign a value to the module.exports |
Reassigning exports can break the module, as it doesn’t overwrite the module.exports | Reassigning module.exports will overwrite exports and should be done carefully |
Use when exporting multiple properties or functions | Use when exporting a single object, function, or value |
Why Export Modules?
- Code Reusability: Allows you to reuse functions, classes, and logic across different parts of your project, reducing duplication.
- Maintainability: Makes it easier to manage and update different parts of your application by separating concerns into individual modules.
- Better Organization: Keeps your codebase clean and organized by grouping related functionality in separate files.
- Easier Debugging: It helps to focus on specific parts of the code where issues occur, making it easier and faster to fix problems.
- Sharing Code: Allows you to share modules with other projects or developers, making it easier to share code with others and work together on projects.
Best Practices for Using Modules in NodeJS
- Export what you need: Export only the necessary parts of your code to keep things modular and avoid exposing unnecessary functionality.
- Use module.exports for single exports: If you are exporting a single function, object, or module use moduensurerts directly.
- Use exports for adding properties: If you are adding multiple functions or methods to the module, using exports is convenient.
- Avoid overwriting exports: Avoid overwriting exports with a new, value as it breaks the reference to module.exports.
Conclusion
Module.exports in NodeJS are used to share code between files, making it easier to organize, reuse, and manage. It allows exporting literals, objects, and functions, helping maintain a clean project structure. Understanding the difference between exports and module.exports ensures proper code sharing across your application.