Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
clear-module
Advanced tools
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.
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);
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.
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 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 a module from the cache
Useful for testing purposes when you need to freshly import a module.
$ npm install clear-module
// foo.js
let i = 0;
module.exports = () => ++i;
const clearModule = require('clear-module');
require('./foo')();
//=> 1
require('./foo')();
//=> 2
clearModule('./foo');
require('./foo')();
//=> 1
Type: string
What you would use with require()
.
Clear all modules from the cache.
Clear all matching modules from the cache.
Type: RegExp
Regex to match against the module IDs.
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.
Type: string
What you would use with require()
.
FAQs
Clear a module from the cache
We found that clear-module demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.