DEV Community

Sherin
Sherin

Posted on

exports vs module.exports in Node.js

exports.id = 1; //this is ok

exports = {id: 1}; //this is not ok

module.exports = {id: 1}; //this is ok

WHY?

exports is simply a variable reference to module.exports. So we can change the properties of the exports object, but if we change the whole object, it would no longer be a reference to module.exports

Top comments (2)

Collapse
 
somedood profile image
Basti Ortiz

Well, that was short and sweet. πŸ‘Œ

Collapse
 
sonntags profile image
Sonntags

Node supports ECMAScript Modules also, but they are currently experimental.
To enable them, add --experimental-modules for node.