Explain the purpose of module.exports?

A module in Node.js is used to encapsulate all the related codes into a single unit of code which can be interpreted by shifting all related functions into a single file. For example, suppose you have a file called greet.js that contains the two functions as shown below:

module.exports = {
greetInHindi: function(){
return “NAMASTE”;
},
greetInKorean: function(){
return “ANNYEONGHASEYO”;
}};
As you can see module.exports provide two functions which can be imported in another file using below code:

var eduGreets = require (“./greet.js”);
eduGreets.greetInHindi() //NAMASTE
eduGreets.greetInKorean() //ANNYEONGHASEYO

Leave a Reply