This is because the value of module.exports
is the value that's returned by require()
within other modules. exports
is just a reference copy of module.exports
offered for convenience.
When you're only modifying (or "augmenting") the export object, either will work as they both refer to that same object. But, once you intend to replace the object, you must set the replacement to module.export
.
From Modules (emphasis mine):
Note that exports
is a reference to module.exports
making it suitable for augmentation only. If you are exporting a single item such as a constructor you will want to use module.exports
directly instead.
function MyConstructor (opts) {
//...
}
// BROKEN: Does not modify exports
exports = MyConstructor;
// exports the constructor properly
module.exports = MyConstructor;