Nodejs Module Caching
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.
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.
If you want to have a module execute code multiple times, then export a function, and call that function.
(https://nodejs.org/api/modules.html#modules_caching)
Trying code
Let’s see bellow test code and the result:
A.js
1 | var list = []; |
index.js
1 | var a1 = require('./A'); |
Result
1 | [ 1 ] |
So, we know a1 and a2 are the same object.