I’ve found the following contract in a Node.js module:
module.exports = exports = nano = function database_module(cfg) {...}
I wonder what’s the difference between module.exports and exports and why both are used here.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Setting
module.exportsallows thedatabase_modulefunction to be called like a function whenrequired. Simply settingexportswouldn’t allow the function to beexported because node exports the object
module.exportsreferences. The following code wouldn’t allow the user to call the function.module.js
The following won’t work.
The following will work if
module.exportsis set.console
Basically node.js doesn’t export the object that
exportscurrently references, but exports the properties of whatexportsoriginally references. Although Node.js does export the objectmodule.exportsreferences, allowing you to call it like a function.2nd least important reason
They set both
module.exportsandexportsto ensureexportsisn’t referencing the prior exported object. By setting both you useexportsas a shorthand and avoid potential bugs later on down the road.Using
exports.prop = trueinstead ofmodule.exports.prop = truesaves characters and avoids confusion.