I am studying how exports and module.exports differ in Node. This question was particularly useful: module.exports vs exports in Node.js. In short... this is what happens:
//require function
module = {
exports:{}
}
(function(exports){
exports = 1; //contents of module
})(module.exports)
If you forget about the node, and just write a simple function with the same functionality:
newmodule = {exports:{}};
function reassign(exports){
exports = ()=>{console.log("123")}
//or exports = 1;
//or exports = "Hello";
}
reassign(newmodule.exports)
console.log(newmodule.exports) // prints empty object, reassign failed
You can observe that newmodule.exports indeed never gets reassigned. Not sure if I am having a brain freeze, but newmodule.exports was passed by reference. Yet, reassigning is not reflected on the passed object newmodule.exports. Why is that?
Extending the passed object works as expected
function reassign(exports){
exports.somefunc = ()=>{console.log("123")} // works as expected
}