A module always loaded in cache. If we only use once, I recommend add strategy to unload the module.
Like the linux kernel do.
To what purpose?
To reduce memory use. @bnoordhuis
I don't think that would make a significant difference. V8 is going to keep the script source in memory anyway.
Maybe V8 can provide API to remove script source. And unload a module and reload is useful for hot patch.
V8 uses the script source whenever it optimizes or deoptimizes a function. It's also needed for Function.prototype.toString() and, because a module in node.js is basically one big function, that means it retains the script source in its entirety.
However, if you want to reload modules, you can do that by manipulating require.cache.
I've always been under the impression that require.cache was an undocumented part of require, but I just now found it, my bad.
Does import cache modules any differently than require? I always thought it was just syntactic sugar over require. I'm attempting to reload modules on file change and clearing the require.cache only seems to work when I use require on the module.
@CoreyLeeLarson If you transpile your modules with Babel, babel converts import into require(), because node currently doesn't support ES6 modules just yet.
@SEAPUNK It must be something else I'm doing wrong, then.
-1 on this, given that the discussion seems to have dropped off, closing. Can reopen if necessary.
Let's re-open. This issue is quite old, however with ES6 modules now, one may desire to invalidate a module that has changed during development. I haven't found any documentation of this feature, only this ticket.
@naturalethic you can go check if an issue on https://github.com/nodejs/modules doesn't already include this and then open one there.
I need this to test a file that has an IIFE on the top level
Most helpful comment
Let's re-open. This issue is quite old, however with ES6 modules now, one may desire to
invalidatea module that has changed during development. I haven't found any documentation of this feature, only this ticket.