Registry / testing / import-fresh

import-fresh

JSON →
library4.0.0jsnpmunverified

Import-fresh bypasses the Node.js module cache to force a fresh import of a module, useful for testing and hot-reloading scenarios. Version 4.0.0 (latest stable, released 2025) requires Node.js ≥22.15 due to its use of ESM module loader hooks. Unlike older cache-clearing approaches (e.g., delete require.cache), import-fresh works with ESM and is the only maintained solution for Node's native ES module system. It uses a unique cache-busting URL per call, which grows memory over time (intended for development only). Sindre Sorhus maintains it with monthly releases.

npm install import-fresh
INSTALL
IMPORT
SIG · IMPORT-FRESH
I
import-fresh
testingjavascriptv4.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.

createImportFresh
import createImportFresh from 'import-fresh'
const createImportFresh = require('import-fresh')
Default export only; ESM-only since v4. CommonJS require will fail.
importFresh (returned function)
const importFresh = createImportFresh(import.meta.url); const mod = await importFresh('./foo.js')
const importFresh = createImportFresh(__filename)
parentURL must be a URL string, not a file path. import.meta.url is the correct argument in ESM.
TypeScript types
import createImportFresh from 'import-fresh'
import { createImportFresh } from 'import-fresh'
The package ships TypeScript types but the export is default, not named. Named import will cause a type error.

Demonstrates creating an importFresh function and using it to bypass the module cache, showing state reset.

import createImportFresh from 'import-fresh'; import { strict as assert } from 'node:assert'; // Simulate a module with mutable state const loadIncrement = () => ` let count = 0; export default function increment() { count += 1; return count; } `; const importFresh = createImportFresh(import.meta.url); // First import const { default: increment } = await importFresh('./fixture.js'); // pretend fixture.js contains loadIncrement() assert.equal(increment(), 1); assert.equal(increment(), 2); // Fresh import resets state const { default: freshIncrement } = await importFresh('./fixture.js'); assert.equal(freshIncrement(), 1); console.log('All tests passed.');
Debug
Known issues
gotchaMemory leak: each call grows the ESM module cache indefinitely.
fix
Use only in development or short-lived processes. Not suitable for production long-running servers.
affects: >=1.0.0
breakingESM-only since v4: require() will throw because the package uses module loader hooks.
fix
Convert to ESM (type: module in package.json or .mjs extension).
affects: >=4.0.0
breakingNode.js version requirement bumped to 22.15 in v4.
fix
Upgrade Node.js to >=22.15 or stay on v3 (which uses older Node.js APIs).
affects: >=4.0.0
gotchaparentURL must be a URL (e.g., import.meta.url), not a file path (__filename).
fix
Pass import.meta.url; for CJS compatibility, use url.pathToFileURL(__filename).
affects: >=4.0.0
deprecatedThe skipNodeModules option is process-global; all calls in the same process must use the same value.
fix
Set skipNodeModules consistently; if you need different behaviors, run in separate processes.
affects: >=4.0.0
Errors
Common errors & fixes
TypeError: createImportFresh is not a function
Default import used as named import: import { createImportFresh } from 'import-fresh'
fix
import createImportFresh from 'import-fresh'
Error [ERR_REQUIRE_ESM]: require() of ES Module /path/to/import-fresh/index.js not supported.
Trying to require() an ESM-only package.
fix
Use import instead of require; ensure your project is ESM (type: module) or use dynamic import().
TypeError [ERR_INVALID_ARG_TYPE]: The "parentURL" argument must be a string.
Passing __filename instead of a URL string.
fix
Pass import.meta.url or new URL(import.meta.url).href
Upgrade
Version history
4.0.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
8 hits · last 30 days
node
8
Resources
import-fresh — npm install import-fresh · libregistry