It is known that each module in Node.js is wrapped in a function, where 5 arguments are passed in (i.e. exports
, module
, require
, __filename
, __dirname
) and the module.exports
object is returned.
Since exports
is just an alias for module.exports
, is there a reason why both module and exports are listed as 2 separate arguments? Wouldn't it be better if it was just module
object being passed in? This would prevent the exports object from being unintentionally reassigned if the user were to do a top-level reassignment (e.g. exports = {x: 5}
).
In terms of design, is it simply for the ease of accessing the exports object? I feel like I'm missing out something here.
I couldn't find this question being answered, but I'm happy to be redirected to an existing one.
// function (exports, module, require, __filename, __dirname) {
const a = 1;
console.log(a);
// return module.exports;
// }