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()
.
Optional filter function. function should return false to prevent the module from being removed from cache.
Use case: To prevent any module in node_modules from being uncached.
clearModule('./my-module', { filter: name => !name.match(/node_modules/) })
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()
.
- import-fresh - Import a module while bypassing the cache
- import-from - Import a module from a given path
- import-cwd - Import a module from the current working directory
- import-lazy - Import a module lazily