Skip to main content

Command Palette

Search for a command to run...

What is the purpose of module.exports?

Updated
1 min read
What is the purpose of module.exports?
R

Full Stack Developer with a passion for building web applications. PHP, Node.js, Laravel, MySQL, MongoDB. Love collaborating & making a difference

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!"

More from this blog

R

Raja Muhammad Asher - Senior Software Engineer - Full Stack Developer

157 posts

Full Stack Developer with a passion for building web applications. PHP, Node.js, Laravel, ExpressJS, MySQL, MongoDB. Love collaborating & making a difference