1
const obj = {A: 2}
module.exports = obj

If another module imports from the above module, will it get a deep copy of obj or just a reference?

  • also [Does module.require(...).* return a copy of module.exports.* or a reference of it?](https://stackoverflow.com/questions/13346046/does-module-require-return-a-copy-of-module-exports-or-a-reference-of-i) – pilchard Nov 24 '22 at 21:06

2 Answers2

2

Exports work exactly the same way as every other way of passing values around.

The value of obj is a reference to the object.

A copy of that value (still a reference) is assigned to exports.

Anything requireing that module will get a copy of that value (still a reference).

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
1

I would say references because modules have their own execution context. meaning they track state of the context as you import / re import it.

He is an article that explain it better. https://krasimirtsonev.com/blog/article/javascript-module-system-for-state-management

Ricardo Silva
  • 1,221
  • 9
  • 19