What is clear-module?
The clear-module npm package allows you to clear a module from the require cache, enabling you to re-require it and get a fresh instance. This is particularly useful in scenarios where you need to reload a module without restarting the Node.js process, such as during development or testing.
What are clear-module's main functionalities?
Clear a single module
This feature allows you to clear a specific module from the require cache. By doing so, the next time you require this module, it will be reloaded as if it was required for the first time.
const clearModule = require('clear-module');
clearModule('./path/to/module');
Clear multiple modules
This feature allows you to clear multiple modules from the require cache in one go. This is useful when you need to refresh several modules at once.
const clearModule = require('clear-module');
['./path/to/module1', './path/to/module2'].forEach(clearModule);
Clear all modules
This feature allows you to clear all modules from the require cache. This can be useful in a development environment where you want to ensure that all modules are reloaded.
const clearModule = require('clear-module');
Object.keys(require.cache).forEach(clearModule);
Other packages similar to clear-module
decache
The decache package provides similar functionality by allowing you to remove a module from the require cache. It is straightforward to use and offers a simple API for clearing modules. Compared to clear-module, decache is more focused on single module clearing and does not provide built-in support for clearing multiple or all modules.
require-reload
The require-reload package allows you to require a module and always get a fresh instance of it. Unlike clear-module, which clears the cache and then requires the module, require-reload handles the re-requiring internally, making it a bit more convenient for certain use cases.
proxyquire
Proxyquire is a more advanced tool that allows you to mock dependencies when requiring modules. While it can be used to achieve similar results to clear-module by mocking and re-requiring modules, it is more complex and offers a broader range of functionalities, including dependency injection and mocking.
clear-module
Clear a module from the cache
Useful for testing purposes when you need to freshly import a module.
Install
$ npm install clear-module
Usage
let i = 0;
module.exports = () => ++i;
const clearModule = require('clear-module');
require('./foo')();
require('./foo')();
clearModule('./foo');
require('./foo')();
API
clearModule(moduleId)
moduleId
Type: string
What you would use with require()
.
clearModule.all()
Clear all modules from the cache.
clearModule.match(regex)
Clear all matching modules from the cache.
regex
Type: RegExp
Regex to match against the module IDs.
clearModule.single(moduleId)
Clear a single module from the cache non-recursively. No parent or children modules will be affected.
This is mostly only useful if you use singletons, where you would want to clear a specific module without causing any side effects.
moduleId
Type: string
What you would use with require()
.
Related