Node.js Exporting and Consuming Modules Invalidating the module cache

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Example

In development, you may find that using require() on the same module multiple times always returns the same module, even if you have made changes to that file. This is because modules are cached the first time they are loaded, and any subsequent module loads will load from the cache.

To get around this issue, you will have to delete the entry in the cache. For example, if you loaded a module:

var a = require('./a');

You could then delete the cache entry:

var rpath = require.resolve('./a.js');
delete require.cache[rpath];

And then require the module again:

var a = require('./a');

Do note that this is not recommended in production because the delete will only delete the reference to the loaded module, not the loaded data itself. The module is not garbage collected, so improper use of this feature could lead to leaking memory.



Got any Node.js Question?