1. clear-module
Clear a module from the cache
clear-module
Package: clear-module
Created by: sindresorhus
Last modified: Sat, 17 Jun 2023 00:13:47 GMT
Version: 4.1.2
License: MIT
Downloads: 2,605,706
Repository: https://github.com/sindresorhus/clear-module

Install

npm install clear-module
yarn add clear-module

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 POST

10 Must-Know Windows Shortcuts That Will Save You Time

10 Must-Know Windows Shortcuts That Will Save You Time

Arrays vs Linked Lists: Which is Better for Memory Management in Data Structures?

Arrays vs Linked Lists: Which is Better for Memory Management in Data Structures?

Navigating AWS Networking: Essential Hacks for Smooth Operation

Navigating AWS Networking: Essential Hacks for Smooth Operation

Achieving Stunning Visuals with Unity's Global Illumination

Achieving Stunning Visuals with Unity's Global Illumination

Nim's Hidden Gems: Lesser-known Features for Writing Efficient Code

Nim's Hidden Gems: Lesser-known Features for Writing Efficient Code