nakwa
nakwa

Reputation: 1213

Impact and alternatives on requiring some of the same packages in every modules of my node.js application?

Let's say I have an application as following:

Questions:

Upvotes: 1

Views: 41

Answers (1)

Aaron Dufour
Aaron Dufour

Reputation: 17505

Yes, you should just require it again. Node caches required modules, so the second time you require something it doesn't actually run that file; it just returns the cached object. So the memory impact is basically 0 (an extra pointer to the same object, more or less) and the execution time is similarly negligible (the cost of a lookup in an object by the module name).

This means that the objects returned by the two requires aren't just identical; they're literally the same object. Any change to one will affect the other. Because of this, you can extend the module in one place and get those extensions everywhere.

Upvotes: 2

Related Questions