How do I export multiple values from a Node.js module?
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.
Verify This Answer
Cross-check this information using these trusted sources:
More FAQs in How module.exports and require Work in Node.js
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.
Still have questions?
Browse all our FAQs or reach out to our support team
