Registry / devops / delete-node-require-cache

delete-node-require-cache

JSON →
library1.9.0jsnpmunverified

A Node.js library (v1.9.0, stable) to clear the require cache for a specified JS file, including parsing and removing cache for its internal dependencies. Useful for hot-reloading or testing scenarios where cached modules must be invalidated. Ships TypeScript types. Minimal overhead with no runtime dependencies. Activates on first call, with subsequent calls being no-ops.

npm install delete-node-require-cache
INSTALL
IMPORT
SIG · DELETE-NODE-REQUIR
D
delete-node-require-cache
devopsjavascriptv1.9.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.

delRequireCache
import { delRequireCache } from 'delete-node-require-cache'
import delRequireCache from 'delete-node-require-cache'
The package exports a named function, not a default export.
delRequireCache
const { delRequireCache } = require('delete-node-require-cache')
const delRequireCache = require('delete-node-require-cache')
CommonJS requires destructuring because the export is a named object.
delRequireCache
import type { delRequireCache } from 'delete-node-require-cache'
Type import for TypeScript projects when only using the type (e.g., typeof).

Demonstrates importing and using delRequireCache to clear the require cache for a specific file.

import { delRequireCache } from 'delete-node-require-cache'; // Path to a JS file that has been required before const filePath = '/path/to/myModule.js'; delRequireCache(filePath); console.log('Cache cleared for', filePath);
Debug
Known issues
gotchaThe function only clears cache for a single file and its direct dependencies. Nested dependencies of those dependencies are not recursively cleared.
fix
Manually handle nested dependencies if required, or call delRequireCache on each root module.
affects: >=1.0.0
gotchaCalling delRequireCache on a file that has never been required will throw an error because the module is not in cache.
fix
Check if the module exists in require.cache before calling, or use a try-catch block.
affects: >=1.0.0
gotchaThe library is ESM-only, but can still be used in CommonJS via dynamic import().
fix
Use dynamic import: import('delete-node-require-cache').then(m => m.delRequireCache(filePath)).
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: delRequireCache is not a function
Incorrect import: using default import instead of named import.
fix
Change import delRequireCache from ... to import { delRequireCache } from ...
Error: Module not found in cache: /path/to/file.js
The specified file has never been required, so it's not in the require cache.
fix
Ensure the file is required before calling delRequireCache, or add error handling.
Upgrade
Version history
1.9.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
7 hits · last 30 days
node
6
Resources
delete-node-require-cache — npm install delete-node-require-cache · libregistry