or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/npm-import-fresh

Import a module while bypassing the cache

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/import-fresh@3.3.x

To install, run

npx @tessl/cli install tessl/npm-import-fresh@3.3.0

index.mddocs/

Import Fresh

Import 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.

Package Information

  • Package Name: import-fresh
  • Package Type: npm
  • Language: JavaScript with TypeScript definitions
  • Installation: npm install import-fresh

Core Imports

CommonJS:

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';

Basic Usage

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)

Capabilities

Import Fresh Function

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:
    • Relative path: './my-module', '../utils/helper'
    • Absolute path: '/path/to/module'
    • Package name: 'lodash', 'express'

Returns:

  • The imported module's exports (any type)

Error Handling:

  • Throws TypeError with message "Expected a string" if moduleId is not a string

Behavior:

  • Resolves the module path using the calling module's directory as context
  • Removes the target module from Node.js's require.cache
  • Properly manages parent-child module relationships to prevent memory leaks
  • Re-imports the module with fresh execution context
  • Works correctly even when the parent module is removed from cache

Usage 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

Types

/**
 * 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.