Registry / testing / require-again

require-again

JSON →
library2.0.0jsnpmunverified

require-again is a lightweight Node.js utility that clears the require cache for a given module path, allowing you to re-require it with fresh state. This is particularly useful in unit tests where you need to test code that behaves differently based on environment variables or other runtime conditions (e.g., NODE_ENV). The package is version 2.0.0, last updated with support for Node.js 4-6. Unlike other cache-busting tools (like decache or rerand), require-again re-adds the module back to the cache after re-requiring, so subsequent normal requires return the cached version. It has zero dependencies and a minimal API.

npm install require-again
INSTALL
IMPORT
SIG · REQUIRE-AGAIN
R
require-again
testingjavascriptv2.0.0
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.

requireAgain
const requireAgain = require('require-again')
import requireAgain from 'require-again'
This package uses CommonJS (require), not ESM. It is not typed for TypeScript.

Demonstrates using requireAgain to re-require a module with a changed environment variable, showing that the cache is bypassed and the module gets a fresh evaluation.

const requireAgain = require('require-again'); const path = require('path'); // Setup: create a test module that reads environment const fs = require('fs'); const testModulePath = path.join(__dirname, 'test-module.js'); fs.writeFileSync(testModulePath, ` module.exports = { env: process.env.NODE_ENV || 'development' }; `); // First require let mod = require(testModulePath); console.log(mod.env); // 'development' // Change environment process.env.NODE_ENV = 'production'; // Re-require with fresh cache mod = requireAgain(testModulePath); console.log(mod.env); // 'production' // Cleanup process.env.NODE_ENV = ''; fs.unlinkSync(testModulePath);
Debug
Known issues
gotcharequireAgain will re-add the module back to the require cache after re-requiring. This means that later normal requires will return the same instance as the one returned by requireAgain.
fix
If you need persistent cache clearance, use decache or manually delete the entry from require.cache before requiring.
affects: >=1.0.0
deprecatedThe package only lists support for Node 4.x, 5.x, and 6.x. It may not work on newer Node versions without issues.
fix
Test with your Node version or consider using a more modern alternative like decache or jest.resetModules().
affects: >=2.0.0
gotcharequireAgain expects a path that is the same string that was used in the original require. If you use relative paths inconsistently, the cache may not be cleared correctly.
fix
Always use the same resolved path (e.g., via path.resolve) when requiring and calling requireAgain.
affects: >=1.0.0
Errors
Common errors & fixes
Error: Cannot find module 'require-again'
Package is not installed.
fix
Run 'npm install require-again'
requireAgain is not a function
Incorrect import: using ES6 import syntax with CommonJS module.
fix
Use 'const requireAgain = require('require-again');'
Upgrade
Version history
2.0.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

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