Difference between exports and module.exports

The module represents the current module in the plain Javascript object.

Exports is a plain JavaScript variable. Module is a plain javascript object which has the exports property

From one module to another, when we want to export a single class, v…


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by DHANUSH N

The module represents the current module in the plain Javascript object.

Exports is a plain JavaScript variable. Module is a plain javascript object which has the exports property

From one module to another, when we want to export a single class, variable, or function, we use modules. exports.

From one module to another, when we want to export multiple variables or functions, we use exports.

var module = { exports: { value1: 10 , value2: 20 } };
var exports = module.exports;

return module.exports;

In the above example, we have assigned multiple values to the same object.

module.exports.value1 returns 10 and module.exports.value2 returns 20

var module = { exports:  10  } };
var exports = module.exports;

return module.exports;

In the preceding example, exports is set to a value that results in modules.Exports are no longer the same object.

module.exports returns 10

const userDetails= (data)=>{
    return {
        getUsername:()=> data.username,
        getEmail:()=> data.email,
   }
}

module.exports = userDetails;

In the preceding example, calling userDetails(data>) returns the value of the username.getUsername()

const getUsername:(data)=> data.username

const getEmail:(data)=> data.email

exports.getUsername = getUsername;
exports.getEmail = getEmail;

In the above example the value of username can be got by getUsername()

If we go with the first approach, we do not need to include new lines in the export statement each time we create a new function.

Whereas in the second approach new functions if created gradually increases the number of lines of the exports statement as well

Therefore, as a best practice in accordance with the NodeJs documentation's usage of exports and module.exports, we can avoid using exports and instead use module.exports.

Thanks for reading ❤️. I hope you liked it.

Follow me via Twitter, Instagram, or Github.


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by DHANUSH N


Print Share Comment Cite Upload Translate Updates
APA

DHANUSH N | Sciencx (2022-11-13T18:11:25+00:00) Difference between exports and module.exports. Retrieved from https://www.scien.cx/2022/11/13/difference-between-exports-and-module-exports/

MLA
" » Difference between exports and module.exports." DHANUSH N | Sciencx - Sunday November 13, 2022, https://www.scien.cx/2022/11/13/difference-between-exports-and-module-exports/
HARVARD
DHANUSH N | Sciencx Sunday November 13, 2022 » Difference between exports and module.exports., viewed ,<https://www.scien.cx/2022/11/13/difference-between-exports-and-module-exports/>
VANCOUVER
DHANUSH N | Sciencx - » Difference between exports and module.exports. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/11/13/difference-between-exports-and-module-exports/
CHICAGO
" » Difference between exports and module.exports." DHANUSH N | Sciencx - Accessed . https://www.scien.cx/2022/11/13/difference-between-exports-and-module-exports/
IEEE
" » Difference between exports and module.exports." DHANUSH N | Sciencx [Online]. Available: https://www.scien.cx/2022/11/13/difference-between-exports-and-module-exports/. [Accessed: ]
rf:citation
» Difference between exports and module.exports | DHANUSH N | Sciencx | https://www.scien.cx/2022/11/13/difference-between-exports-and-module-exports/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.