Junior — Middle
Is it possible to delete or reset cache contents during program execution?
sobes.tech AI
Answer from AI
In Node.js, it is possible to delete or reset the contents of the module cache during runtime. This is done through the require.cache object, which stores loaded modules. To remove a module from the cache, simply delete the corresponding key from require.cache.
Example of removing a module from the cache:
// Path to the module
const modulePath = require.resolve('./myModule');
// Remove the module from the cache
delete require.cache[modulePath];
// Now, the next time require is called, the module will be loaded again
const myModule = require('./myModule');
This is useful, for example, during development when you need to reload a module without restarting the entire application. However, it should be used with caution to avoid breaking the integrity of the program's state.