Difference between "module.exports" and "exports" in the CommonJs Module System
Also, one things that may help to understand:
math.js
this.add = function (a, b) {
return a + b;
};
client.js
var math = require('./math');
console.log(math.add(2,2); // 4;
Great, in this case:
console.log(this === module.exports); // true
console.log(this === exports); // true
console.log(module.exports === exports); // true
Thus, by default, "this" is actually equals to module.exports.
However, if you change your implementation to:
math.js
var add = function (a, b) {
return a + b;
};
module.exports = {
add: add
};
In this case, it will work fine, however, "this" is not equal to module.exports anymore, because a new object was created.
console.log(this === module.exports); // false
console.log(this === exports); // true
console.log(module.exports === exports); // false
And now, what will be returned by the require is what was defined inside the module.exports, not this or exports, anymore.
Another way to do it would be:
math.js
module.exports.add = function (a, b) {
return a + b;
};
Or:
math.js
exports.add = function (a, b) {
return a + b;
};
module
is a plain JavaScript object with an exports
property. exports
is a plain JavaScript variable that happens to be set to module.exports
.
At the end of your file, node.js will basically 'return' module.exports
to the require
function. A simplified way to view a JS file in Node could be this:
var module = { exports: {} };
var exports = module.exports;
// your code
return module.exports;
If you set a property on exports
, like exports.a = 9;
, that will set module.exports.a
as well because objects are passed around as references in JavaScript, which means that if you set multiple variables to the same object, they are all the same object; so then exports
and module.exports
are the same object.
But if you set exports
to something new, it will no longer be set to module.exports
, so exports
and module.exports
are no longer the same object.
Renee's answer is well explained. Addition to the answer with an example:
Node does a lot of things to your file and one of the important is WRAPPING your file. Inside nodejs source code "module.exports" is returned. Lets take a step back and understand the wrapper. Suppose you have
greet.js
var greet = function () {
console.log('Hello World');
};
module.exports = greet;
the above code is wrapped as IIFE(Immediately Invoked Function Expression) inside nodejs source code as follows:
(function (exports, require, module, __filename, __dirname) { //add by node
var greet = function () {
console.log('Hello World');
};
module.exports = greet;
}).apply(); //add by node
return module.exports; //add by node
and the above function is invoked (.apply()) and returned module.exports. At this time module.exports and exports pointing to the same reference.
Now, imagine you re-write greet.js as
exports = function () {
console.log('Hello World');
};
console.log(exports);
console.log(module.exports);
the output will be
[Function]
{}
the reason is : module.exports is an empty object. We did not set anything to module.exports rather we set exports = function()..... in new greet.js. So, module.exports is empty.
Technically exports and module.exports should point to same reference(thats correct!!). But we use "=" when assigning function().... to exports, which creates another object in the memory. So, module.exports and exports produce different results. When it comes to exports we can't override it.
Now, imagine you re-write (this is called Mutation) greet.js (referring to Renee answer) as
exports.a = function() {
console.log("Hello");
}
console.log(exports);
console.log(module.exports);
the output will be
{ a: [Function] }
{ a: [Function] }
As you can see module.exports and exports are pointing to same reference which is a function. If you set a property on exports then it will be set on module.exports because in JS, objects are pass by reference.
Conclusion is always use module.exports to avoid confusion. Hope this helps. Happy coding :)