Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
What comes to your mind when you hear the word "Export"? Ofcourse, in real life, export means sending goods from one place to another. The same thing happens in the world of coding. Whenever we use the term export, we mean sending any variables, objects, or literals from one place to another.
You must be confused about why we are talking so much about export today. The reason behind this is today's topic of discussion is how to create and export modules in Node.js.
Modules
Before creating and exporting modules, let's first understand the modules. A module is simply a segment of JavaScript code which can be reused. It could be a .js file or a directory of .js files. The content in these files can be exported and used in other files.
Let's first try a simple Javascript function. Then we will move to the module creation and module export. We will create a file with the name index.js and make the required changes inside that.
index.js
JS
JS
function concatenation(x, y) { return x + y; }
var myWord = concatenation("Coding ", "Ninjas"); console.log(myWord);
You can also try this code with Online Javascript Compiler
In the above program, we just created a concatenation function that adds two string values, as shown in the output screen. Later on, we created a variable named myWord and called the function. And, at last, we are printing the concatenated result.
Defining functions inside the main index.js works when you are a newbie or working at a beginner level, but in huge projects, it is not possible to create all the functions inside the main function. Defining and using functions in the main function may make a mess. Hence the term modules came into existence.
Create Module
As we are familiar with the reason behind the modules' existence, let us learn how we can create a module inside node JS.
To do so, we just have to create a new file, module.js, and then write the same function there. Let's have a look.
module.js
JS
JS
function concatenation(x, y) { return x + y; }
You can also try this code with Online Javascript Compiler
Error! "concatenation is not defined". Just by doing the cut-paste task, we can face an error like this. We are facing this error because we have not yet linked our module.js file to the index.js file.
Import Module
A module can be imported, and its functionality can be reused. Node.js has the built-in require() function, which is the simplest way to use modules from the existing separate files. The basic functionality of require() is that it reads a JavaScript file, executes it, and proceeds to return the export object.
Hence, we will use a require() function to resolve the above error. Let's try again the same example with the require() function.
module.js
JS
JS
function concatenation(x, y) { return x + y; }
You can also try this code with Online Javascript Compiler
Oops! Again an error? But the error is different here. The error says, "myModule.concatenation is not a function". The program is showing an error because we have to call the export() function to use any function outside the file.
Export Module
The module.exports is a special object of a Javascript file in Node.js by default. Here the module is a variable that defines the current modules. And the exports() function is an object that will be exported as a module. So whatever we assign to the module.exports, it will be exported as a module.
We can resolve the error that came in the last section by using the exports() function. Let's have a look.
module.js
JS
JS
function concatenation(x, y) { return x + y; }
module.exports.concatenation = concatenation;
You can also try this code with Online Javascript Compiler
As you can see, both functions are exported separately in the module.js file. But writing the same lines for each function in a huge project will be a tough task.
To overcome this problem, we have the array format also. Let's have a look.
Array Format
In the array format, we can export multiple functions at a time just by writing the function name. Let's quickly move to the example for better understanding.
module.js
JS
JS
function concatenation(x, y) { return x + y; }
function greet(x) { return x; }
module.exports = { concatenation, greet };
You can also try this code with Online Javascript Compiler
The output will remain the same for the array format also. Additionally, this will save time too for the user. Hence, this is a good practice to use the array format instead of a single format.
As we have now discussed all our topics, let's now discuss some faqs.
Frequently Asked Questions
What is module export in Node.js?
Module export in Node.js allows sharing functions, objects, or variables from one file to another using module.exports or exports.
What is the difference between module.exports and exports?
module.exports is the actual object exported from a module, while exports is a shorthand reference that points to module.exports by default.
How to export a module function in JavaScript?
Export a function in JavaScript using module.exports = functionName; or exports.functionName = functionName; in Node.js, or export in ES6 modules.
Conclusion
This article discusses the topic of how to create and export modules in node js. In detail, we have seen the definition module. Along with this, we have seen how to create, import and export the modules with examples.
We hope this blog has helped you enhance your knowledge to create and export modules in node js. If you want to learn more, then check out our articles.
But suppose you have just started your learning process and are looking for questions from tech giants like Amazon, Microsoft, Uber, etc. In that case, you must look at the problems, interview experiences, and interview bundles for placement preparations.
However, you may consider our paid courses to give your career an edge over others!
Live masterclass
Google SDE interview process: Strategies to succeed
by Ravi Raj Singh
24 Mar, 2025
01:30 PM
Transition to an Amazon SDE role: Get insider tips
by Anubhav Sinha
26 Mar, 2025
01:30 PM
Microsoft Data Analytics interview: Dos and Don’ts to get shortlisted
by Prerita Agarwal
25 Mar, 2025
01:30 PM
Become MAANG Data Analyst: PowerBI & AI for Data Visualization
by Alka Pandey
27 Mar, 2025
01:30 PM
Google SDE interview process: Strategies to succeed
by Ravi Raj Singh
24 Mar, 2025
01:30 PM
Transition to an Amazon SDE role: Get insider tips