You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
require: How to require a file and will not automatically append to the cache? #689
You can remove it from the cache manually. But since you should require only when you are initializing your app (it's sync!), the cache should have fixed size after your app is loaded.
Why are you concerned about the cache becoming "bigger and bigger?" It's almost always just object references, so it's not taking up as much memory as you are likely thinking. So even if you delete require.cache['/path/to/my/module'], you're just deleting a reference. If you are already referencing the module in your code (e.g. via var mymodule = require('mymodule');), then deleting the cache reference has minimal impact on memory usage.
Also: require() already only reads, parses, and executes a module one time. So var foo = require('foo'), bar = require('foo'); will assign the exact same object to both foo and bar. That means if you mutate the object like bar.baz = 5;, those same changes will be reflected in foo.
The internals of require(...) cache the module object to ensure it only needs to be loaded once.You can remove the reference from require.cache but, as @mscdex stated, it's just a reference. It's up to the garbage collector to decide when the object is no longer accessible.
I'm closing this issue. If you feel your question is not sufficiently answered, feel free to re-open it.
Activity
madbence commentedon Feb 2, 2015
You can remove it from the cache manually. But since you should
requireonly when you are initializing your app (it's sync!), the cache should have fixed size after your app is loaded.mscdex commentedon Feb 2, 2015
Why are you concerned about the cache becoming "bigger and bigger?" It's almost always just object references, so it's not taking up as much memory as you are likely thinking. So even if you
delete require.cache['/path/to/my/module'], you're just deleting a reference. If you are already referencing the module in your code (e.g. viavar mymodule = require('mymodule');), then deleting the cache reference has minimal impact on memory usage.Also:
require()already only reads, parses, and executes a module one time. Sovar foo = require('foo'), bar = require('foo');will assign the exact same object to bothfooandbar. That means if you mutate the object likebar.baz = 5;, those same changes will be reflected infoo.Qard commentedon Feb 2, 2015
The internals of
require(...)cache the module object to ensure it only needs to be loaded once.You can remove the reference fromrequire.cachebut, as @mscdex stated, it's just a reference. It's up to the garbage collector to decide when the object is no longer accessible.I'm closing this issue. If you feel your question is not sufficiently answered, feel free to re-open it.