Registry / testing / clear-module

clear-module

JSON →
library4.1.3jsnpmunverified

Clear a module from the Node.js require cache, forcing a fresh reload on the next require. Current stable version is 4.1.3 (last updated 2020, Node >=8). It recursively clears the target module and all its parent modules from the cache. Alternatives like delete require.cache[require.resolve(...)] do not handle recursive clearing. Common use case: testing where modules have state or need to be reloaded. Ships TypeScript definitions. Low maintenance, no dependencies.

npm install clear-module
INSTALL
IMPORT
SIG · CLEAR-MODULE
C
clear-module
testingjavascriptv4.1.3
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

clearModule
import clearModule from 'clear-module'
const clearModule = require('clear-module')
Package is ESM-only since v4. For CommonJS, use dynamic import or stick with v3.
clearModule.all
clearModule.all()
Clears all modules from the cache. No arguments required.
clearModule.match
clearModule.match(/regex/)
clearMatch(/regex/)
Takes a RegExp to match module IDs. For string patterns, construct a RegExp.
clearModule.single
clearModule.single(moduleId)
clearModule.single(moduleId, true)
Clears only the specified module, not its parents or children. Added in v4.

Demonstrates using clear-module to reset a module's cached exports. Shows ESM import, creating a require, and verifying fresh state.

import clearModule from 'clear-module'; import { createRequire } from 'module'; const require = createRequire(import.meta.url); // Create a module with state require.cache = {}; const modPath = './test-module.js'; // Write a temporary module import { writeFileSync, unlinkSync } from 'fs'; writeFileSync('./test-module.js', 'let count = 0; module.exports = () => ++count;'); try { const fn1 = require(modPath); console.log(fn1()); // 1 console.log(fn1()); // 2 clearModule(modPath); const fn2 = require(modPath); console.log(fn2()); // 1 (fresh) } finally { unlinkSync('./test-module.js'); }
Debug
Known issues
breakingESM-only in v4. CommonJS require() will throw.
fix
Use import or upgrade to Node >=12.22.0 with --experimental-modules flag if needed, or downgrade to v3.
affects: >=4.0.0
breakingNode version minimum raised to v8 (v3 required node >=6, v4 requires node >=8).
fix
Upgrade Node.js to v8 or higher.
affects: >=4.0.0
deprecatedThe 'all' method with no arguments still works but might be deprecated in future.
fix
Use clearModule.all() only when needed; prefer targeted clears.
affects: >=4.0.0
gotchaDoes not clear built-in modules (node:fs, etc.) or modules loaded via ESM import (only affects CJS require cache).
fix
For ESM cache clearing, use dynamic import with cache busting (e.g., import(`${url}?update=${Date.now()}`)).
affects: >=1.0.0
Errors
Common errors & fixes
SyntaxError: Unexpected token 'export'
Using CommonJS require() with ESM-only v4 import syntax.
fix
Use dynamic import: const clearModule = (await import('clear-module')).default;
Cannot find module 'clear-module'
Package not installed or not in node_modules path.
fix
Run `npm install clear-module` in your project root.
TypeError: clearModule is not a function
Incorrect import when using default export: the package exports a default function, but some users mistakenly import as a namespace.
fix
Use `import clearModule from 'clear-module'` (default import) not `import * as clearModule from 'clear-module'`.
Error: Cannot find module './foo'
Module ID passed to clearModule does not match what require() expects (missing ./ or path issue).
fix
Ensure moduleId is the same string you pass to require(), including relative path (e.g., './foo').
Upgrade
Version history
4.1.3latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
13 hits · last 30 days
node
10
Amazon
1
OpenAI (training)
1
Resources
clear-module — npm install clear-module · libregistry