The entries in this cache are garbage collection when I no longer use the module?
Not. Modules loaded with require() are cached indefinitely, whether you execute them or not.
The memory for the Javascript variables / objects used by the module is garbage collection that obeys all normal garbage collection rules (when there is no live code that still has a reference to the variable / object). But the module cache stores a link to the loaded module, so the code or any variables of the module level are not garbage collected unless the module is manually removed from the cache.
Here's a link to the node.js doc on the topic.
Caching
Modules are cached after the first boot. This means (by the way) that every call required ('foo') will receive exactly the same returned object if it resolves to the same file.
If you want to manually remove the module from the cache, which is described here:
code / module unloading
Although, this will allow you to collect junk files of all level variables, given that the structure of node.js is structured. I do not think that it really will unload the code from memory.
source share