Caching

  1. Modules are cached after the first time they are loaded. This means (among other things) that every call to require('foo') will get exactly the same object returned, if it would resolve to the same file.

  2. Multiple calls to require('foo') may not cause the module code to be executed multiple times. This is an important feature. With it, partially done objects can be returned, thus allowing transitive dependencies to be loaded even when they would cause cycles.

  3. If you want to have a module execute code multiple times, then export a function, and call that function.

  4. Modules are cached based on their resolved filename. Since modules may resolve to a different filename based on the location of the calling module (loading from node_modules folders), it is not a guarantee that require('foo') will always return the exact same object, if it would resolve to different files.

Casiano Rodriguez León 2015-01-07