What is decache?
The decache npm package is designed to delete modules from the cache in Node.js. This is particularly useful during development when you want to ensure that the latest version of a module is reloaded without having to restart the Node.js process.
What are decache's main functionalities?
Module cache deletion
This feature allows you to remove a specific module from the Node.js require cache. After decaching, the next time you require the module, Node.js will reload it from the filesystem, ensuring you have the latest version without restarting your application.
const decache = require('decache');
decache('./myModule');
let myModule = require('./myModule'); // This will reload the module from file
Other packages similar to decache
clear-require
clear-require is similar to decache in that it clears modules from the require cache. It provides a simple API to clear a specific module or all modules at once. Unlike decache, clear-require also supports clearing modules by a regex pattern.
require-reload
require-reload offers functionality to reload a module by requiring it again without needing to clear the cache manually. It is a bit more direct in usage compared to decache, as it wraps the require function to automatically decache the module.
import-fresh
import-fresh is designed to import a module by bypassing the cache. It's similar to decache but is more focused on ES6 module syntax compatibility. It's useful for cases where you want to ensure a fresh state of the module on each import.
decache
In node.js when you require()
a module, node stores a cached version of the
module, so that all subsequent calls to require()
do not have to reload
the module from the filesystem.
decache
( Delete Cache ) lets you delete modules from node.js require()
cache
this is useful when testing your modules/projects.
Why?
When testing our modules we often need to re-require the module being tested.
This makes it easy.
What?
An easy way to delete a cached module.
How? (usage)
### install
Install the module from npm:
npm install decache --save-dev
Use it in your code:
var decache = require('decache');
var mymod = require('./mymodule.js');
console.log(mymod.count());
console.log(mymod.incrementRunCount());
decache('./mymodule.js');
mymod = require('./mymodule.js');
console.log(mymod.count());
Modules other than .js
, like for example, .jsx
, are supported as well.
If you have any questions or need more examples, please create a GitHub issue:
https://github.com/nelsonic/decache/issues
Thanks!