A module is an application’s component that can hold the partial programs to provide some custom functionality. It decreases the clatter in the code. Also, it’s a bad approach to write the entire code for a program in a single file, so it is suggested to divide the relevant code into separate JavaScript files called modules.
Introduction to Module Exporting in NodeJS
A NodeJS application can contain many JavaScript files which hold code for some functionality(called modules), these modules have to be imported into the main server file to implement its functionality. For importing the module’s methods and properties we have to expose them using export.
For example, if a company has an application in which it is required to send an email, the developer of the application creates a new JavaScript file to write the code for a function that can use to send emails and to import that function of sending email in the main server file or any other JavaScript file it is necessary to export it first. Let’s see the methods of exporting modules.
The 2 Modules for Exporting in Node.js
The JavaScript modules are exported using two methods ‘module.exports’ and ‘exports’.
Using module.exports to Export Modules
This method can occur once in a file, if we try to use it to export another method the previous one will not be able to export its method, basically it overwrites the exports with the new one.
For exporting multiple methods using ‘module.exports’, it is suggested to use it at the end of the code with a curly bracket containing the methods and property to be exported.
Syntax of module.exports
module.exports = { functionName }
where functionName is the name of a function present in the current file to be exported.
Example:
const addTwoNum = (a, b) => {
return a + b;
}
module.exports = { addTwoNum };
Using exports to Export Modules Multiple Times
This method can occur multiple times in a file to export methods and properties. It is recommended for circumstances where it is required to export multiple times throughout the program rather than just at the end.
Syntax of the exports module
exports.functionName = functionName
where functionName is the name of a function present in the current file to be exported.
Example:
const addTwoNum = (a, b) => {
return a + b;
}
exports.addTwoNum = addTwoNum;
Import the Module
The module and its methods which are exported can be directly imported inside any other JavaScript file using require() method.
Example:
We have a module ‘sum.js’ which contains a method to take two values and return the sum. We have used ‘exports.module’ to export this method.
sum.js
const addTwoNum = (a, b) => {
return a + b;
}
module.exports = { addTwoNum };
The addTwoNum method is available to import into another JavaScript file for using it. To import the module in another JavaScript file in Node.js, we have to use require() method and pass the module location inside it. We can also use curly brackets {} to import some particular methods.
app.js
let { addTwoNum } = require('./sum');
let sum = addTwoNum(1, 2);
console.log("Sum:", sum); //3
Here we have imported the addTwoSum method from the ‘./sum’ module. It is not required to add the extension in the file path.
Output:
Sum: 3
Examining Exported Methods and Properties in a Node.js Module
We can use the module object to check for the methods and properties in a particular module that is exported.
Below is the code to see which method of the module is exported.
const hello = () => {
return "Hello World";
}
module.exports = { hello };
console.log(module);
Output:
Module {
id: '.',
path: 'C:\\Users\\ag290\\OneDrive\\Desktop\\exports',
exports: { hello: [Function: hello] },
parent: null,
filename: 'C:\\Users\\ag290\\OneDrive\\Desktop\\exports\\sum.js',
loaded: false,
children: [],
paths: [
'C:\\Users\\ag290\\OneDrive\\Desktop\\exports\\node_modules',
'C:\\Users\\ag290\\OneDrive\\Desktop\\node_modules',
'C:\\Users\\ag290\\OneDrive\\node_modules',
'C:\\Users\\ag290\\node_modules',
'C:\\Users\\node_modules',
'C:\\node_modules'
]
}
Here you can notice that the global object returns many properties, the one we want is the exports property that contains the function ‘hello’, which indicates that it is successfully exported.
Summary
In Node.js, modules are a way to organize your code and reuse it across your application. You can use module.exports or exports to specify which methods and properties from a given file should be made available to other parts of the application.
To use a method or property from a module in another file, you can use the require() function and pass in the path to the file containing the module.
Modules help you to keep your code organized and reduce clutter, making it easier to develop and maintain your application.
Reference
https://developer.mozilla.org/en-US/docs/web/javascript/reference/statements/export
For more information, click here