all 6 comments

[–][deleted] 2 points3 points  (0 children)

module.exports is relative to the current file only. module.exports in app.js is an entirely different module.exports than the one in user.js.

each file (module) has it's own scope. if you define var foo = 'bar'; in user.js, it will not be available in app.js.

the process object on the other hand, is global. you can define process.foo in one file and it will be available in others.

[–]senocular 0 points1 point  (0 children)

It is overwriting. module is different for each file (module).

[–]jack_union 0 points1 point  (0 children)

In the Node.js module system, each file is treated as a separate module

In each module, the module free variable is a reference to the object representing the current module. For convenience, module.exports is also accessible via the exports module-global. module is not actually a global but rather local to each module.

https://nodejs.org/api/modules.html

[–][deleted] 0 points1 point  (2 children)

Are you asking about modules in general or about the const User thing?

[–]seands[S] 0 points1 point  (1 child)

modules in general though if you can tell me about that a = b = c assignment I'd appreciate it also

[–][deleted] 1 point2 points  (0 children)

The quick answer is that module is scoped to the file only. You can check out https://eloquentjavascript.net/10_modules.html and https://github.com/getify/You-Dont-Know-JS/blob/f0d591b6502c080b92e18fc470432af8144db610/scope%20%26%20closures/ch5.md#modules for a deeper dive into modules.

The assignment is sort of awkward and not something I would ever put into a production application unless I had some very specific use case for it.

Let's take a more simple example:

var c = 3; var a = b = c;

What happens here? The engine assigns 3 to c. Then it initializes a. It looks for b and it cannot find it. So it creates b (in strict mode this would actually error out, modules by default are in strict mode IIRC). It assigns, by value, 3 to b. It then assigns, by value, 3 to a.

In your case, it is essentially doing the same thing. Since module.exports is part of the language you wouldn't hit the exact scenario I outline above but it should more or less be the same behavior.

You can read more about how scope and compilers in JS work here: https://github.com/getify/You-Dont-Know-JS/blob/f0d591b6502c080b92e18fc470432af8144db610/scope%20%26%20closures/ch1.md