Import a module while bypassing the cache
npx @tessl/cli install tessl/npm-import-fresh@3.3.0Import Fresh provides a utility function for importing Node.js modules while bypassing the require cache. This enables fresh imports of modules for testing and development purposes by ensuring that modules are fully reloaded and re-executed, resetting any module-level state.
npm install import-freshCommonJS:
const importFresh = require('import-fresh');ESM:
import importFresh from 'import-fresh';TypeScript (CommonJS):
import importFresh = require('import-fresh');TypeScript (ESM):
import importFresh from 'import-fresh';const importFresh = require('import-fresh');
// Example: Fresh import of a counter module
// counter.js exports a function that increments an internal counter
require('./counter')(); //=> 1
require('./counter')(); //=> 2 (cached, counter state persists)
importFresh('./counter')(); //=> 1 (fresh import, counter state reset)
importFresh('./counter')(); //=> 1 (fresh import again, counter state reset)Imports a module while bypassing Node.js require cache, ensuring the module is freshly loaded and executed.
/**
* Import a module while bypassing the cache
* @param moduleId - The module identifier to import (relative path, absolute path, or package name)
* @returns The imported module's exports
* @throws TypeError if moduleId is not a string
*/
function importFresh(moduleId: string): any;Parameters:
moduleId (string): The module identifier to import. Can be:
'./my-module', '../utils/helper''/path/to/module''lodash', 'express'Returns:
Error Handling:
TypeError with message "Expected a string" if moduleId is not a stringBehavior:
require.cacheUsage Examples:
const importFresh = require('import-fresh');
// Testing scenario: Module with internal state
// config.js
let config = { debug: false };
module.exports = {
getConfig: () => config,
setDebug: (value) => { config.debug = value; }
};
// Test file
const config1 = require('./config');
config1.setDebug(true);
console.log(config1.getConfig().debug); //=> true
const config2 = require('./config'); // Cached
console.log(config2.getConfig().debug); //=> true (state persisted)
const config3 = importFresh('./config'); // Fresh import
console.log(config3.getConfig().debug); //=> false (state reset)// TypeScript usage with generic type parameter (CommonJS)
import importFresh = require('import-fresh');
interface MyModule {
getValue(): number;
reset(): void;
}
const myModule = importFresh<MyModule>('./my-module');
const value = myModule.getValue(); // Fully typed// TypeScript usage with generic type parameter (ESM)
import importFresh from 'import-fresh';
interface MyModule {
getValue(): number;
reset(): void;
}
const myModule = importFresh<MyModule>('./my-module');
const value = myModule.getValue(); // Fully typed/**
* Import a module while bypassing the cache with TypeScript generics support
*/
declare function importFresh<T>(moduleId: string): T;The TypeScript declaration supports generic type parameters for type-safe imports when you know the expected module structure.