How module.exports and require Work in Node.js
module.exports and require are the core of Node.js's CommonJS module system. Here is how they work.
How module.exports and require Work in Node.js
module.exports and require are the core of Node.js's CommonJS module system. Here is how they work.
What module.exports Does
module.exports is an object that Node.js gives every file. Whatever you assign to it becomes what require returns when another file requires this one. You export functions, objects, or values by assigning them to module.exports.
What require Does
require takes a file path, runs that file, and returns its module.exports. The file's code runs once and is cached, so requiring the same file twice returns the same object.
Exporting a Single Value
Assign directly to module.exports: module.exports = function add(a, b) { return a + b }. The requiring file gets the function.
Exporting Multiple Values
Attach to the exports object: exports.add = function() {}; exports.subtract = function() {}. The requiring file gets an object with both. Do not assign a new object to module.exports if you use exports.add, since they are separate references.
The Common Mistake
Mixing exports.add and module.exports = in the same file. exports is a reference to module.exports initially, but reassigning module.exports breaks the link. Pick one approach per file.
Caching
Modules are cached after their first require. This means a module's code runs once. If you need fresh state, export a factory function that creates new state, not a shared object.
The Takeaway
module.exports holds what a file exports, and require returns it. Export a single value by assigning to module.exports, or multiple values with exports.add. Avoid mixing the two, and remember modules are cached after first require.
module.exports is an object Node.js gives every file. Whatever you assign to it becomes what require returns when another file requires this one. You can export a single value by assigning directly, or multiple values with exports.add.
require takes a file path, runs that file, and returns its module.exports. The file's code runs once and is cached, so requiring the same file twice returns the same object, not a fresh copy.
Mixing exports.add and module.exports = in the same file. exports is a reference to module.exports initially, but reassigning module.exports breaks the link. Pick one approach per file to avoid bugs.
Yes. Modules run once and are cached after their first require. Requiring the same file twice returns the same object. If you need fresh state, export a factory function that creates new state, not a shared object.
Attach to the exports object: exports.add = function() {}; exports.subtract = function() {}. The requiring file gets an object with both. Do not assign a new object to module.exports if you use exports.add.
Ready to master Node.js completely?
Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course to dive deeper with high-quality video tutorials, solve interview questions, and a premium community.
Master Node.js
Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course.

