Skip to content

Clear a module from the cache

License

Notifications You must be signed in to change notification settings

sindresorhus/clear-module

Folders and files

NameName
Last commit message
Last commit date

Latest commit

f68a96b · Nov 3, 2021

History

29 Commits
Jan 1, 2021
May 21, 2017
Jul 11, 2018
Jul 11, 2018
Jul 11, 2018
Mar 9, 2020
Mar 9, 2020
Nov 3, 2021
Jan 28, 2017
Nov 3, 2021
Jan 28, 2017
Feb 22, 2020
Nov 3, 2021
Apr 8, 2019
Feb 22, 2020
Nov 3, 2021
Jan 1, 2021
Nov 3, 2021

Repository files navigation

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

// foo.js
let i = 0;
module.exports = () => ++i;
const clearModule = require('clear-module');

require('./foo')();
//=> 1

require('./foo')();
//=> 2

clearModule('./foo');

require('./foo')();
//=> 1

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