Registry / testing / require-and-forget

require-and-forget

JSON →
library1.0.1jsnpmunverified

Require a Node module and immediately delete it from the module cache, forcing re-evaluation on the next require call. v1.0.1 is the latest stable version (no updates since 2017). Useful for testing scenarios where you need fresh module state (e.g., random values, mutable singletons). Key differentiator: lightweight and focused solely on cache eviction, unlike heavier testing utilities. Works with Node >=6, CommonJS only.

npm install require-and-forget
INSTALL
IMPORT
SIG · REQUIRE-AND-FORGET
R
require-and-forget
testingjavascriptv1.0.1
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.

default (function)
✓ const forget = require('require-and-forget')
CommonJS only; no ESM or named export. The module exports a single function.

Demonstrates forcing re-evaluation of a module by removing it from cache after each require.

const forget = require('require-and-forget'); // Assume random.js exists: module.exports = Math.random() const r1 = forget('./random'); const r2 = forget('./random'); console.log(r1 !== r2); // true - module not cached
Debug
Known issues
gotchaOnly works with CommonJS (require), not ES modules (import).
fix
Use dynamic import() if you need ESM-like behavior, but caching behavior differs.
affects: >=1.0.0
gotchaModule is removed from cache after require, so subsequent normal require() calls will re-evaluate it.
fix
If you want to keep the module cached, use regular require() instead.
affects: >=1.0.0
gotchaAbsolute or relative paths work; module resolution follows Node's algorithm.
fix
Use paths as you would with require().
affects: >=1.0.0
Errors
Common errors & fixes
Error: Cannot find module './random'
Path passed to forget() is incorrect or file does not exist.
fix
Ensure the path is relative to the calling file (not the module's directory) and the file exists.
TypeError: forget is not a function
Importing incorrectly (e.g., using destructured or ESM import).
fix
Use const forget = require('require-and-forget');
Upgrade
Version history
1.0.1latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
4 hits · last 30 days
node
4
Resources
require-and-forget — npm install require-and-forget · libregistry