Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

require: How to require a file and will not automatically append to the cache? #689

Closed
fundon opened this issue Feb 2, 2015 · 3 comments
Labels
question Issues that look for answers.

Comments

@fundon
Copy link
Contributor

fundon commented Feb 2, 2015

Just question, I know it will be automatically appended to the cache when requiring.
And The cache will become bigger and bigger.

But sometimes I just want to load-once a file, How do to that?

Provide new API?

@madbence
Copy link

madbence commented Feb 2, 2015

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.

@mscdex
Copy link
Contributor

mscdex commented 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. 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.

@Fishrock123 Fishrock123 added the question Issues that look for answers. label Feb 2, 2015
@Qard
Copy link
Member

Qard commented 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 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Issues that look for answers.
Projects
None yet
Development

No branches or pull requests

5 participants