Raja Muhammad Asher
Raja Muhammad Asher

Follow

Raja Muhammad Asher

Follow
What is the purpose of module.exports?

Photo by Max Chen on Unsplash

What is the purpose of module.exports?

Raja Muhammad Asher's photo
Raja Muhammad Asher
·Oct 4, 2022·

1 min read

module.exports is the object that's returned as the result of a require call. You can use it to export a single value or a list of values. It helps in achieving modular programming.

// Define a value.
const myValue = 'Hello, World!';

// Export the value.
module.exports = myValue;

Then, in another file, you can use require to access that value:

// Load the value.
const myValue = require('./path/to/my/module');

console.log(myValue);  // Outputs: "Hello, World!"
 
Share this