What is the purpose of module.exports in Node.js?

 A module encapsulates related code into a single unit of code. This can be interpreted as moving all related functions into a file. Imagine that we created a file called greetings.js and it contains the following two functions:

Image for post

In the above code, module.exports exposes two functions to the outer world. We can import them in another file as follow:

Image for post

Comments

Popular posts from this blog

What is an Event loop in Node.js and how does it work?

What is promise, promise.all and promise.race? How do you chain promises?