Implementation:Promptfoo Promptfoo ESM Loader
| Knowledge Sources | |
|---|---|
| Domains | Module_System, Runtime |
| Last Updated | 2026-02-14 07:45 GMT |
Overview
Concrete tool for loading ESM and CJS modules dynamically, resolving wrapper script paths, and handling module system interoperability across development and production environments.
Description
The ESM_Loader module (esm.ts) provides utilities for dynamic module loading that work correctly in both ESM and CJS contexts. It handles importing user-defined JavaScript/TypeScript modules (custom providers, assertions, transforms), resolving paths to language-specific wrapper scripts (Python, Ruby, Go), and providing the current directory reference that works across bundled and source environments. Results are cached for performance.
Usage
Import these utilities when code needs to dynamically load user-provided modules or locate wrapper scripts for non-JavaScript providers.
Code Reference
Source Location
- Repository: Promptfoo_Promptfoo
- File: src/esm.ts
- Lines: 1-461
Signature
export type WrapperType = 'python' | 'ruby' | 'golang';
export function getDirectory(): string
export function getWrapperDir(type: WrapperType): string
export async function importModule(
modulePath: string,
functionName?: string
): Promise<any>
Import
import { getDirectory, getWrapperDir, importModule } from './esm';
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| type | WrapperType | Yes | Language wrapper type: 'python', 'ruby', or 'golang' |
| modulePath | string | Yes | Path to the module to import (file:// or relative) |
| functionName | string | No | Specific named export to retrieve |
Outputs
| Name | Type | Description |
|---|---|---|
| directory | string | Absolute path to the current module directory |
| wrapperDir | string | Absolute path to the wrapper scripts directory |
| module | any | The imported module or specific exported function |
Usage Examples
import { importModule, getWrapperDir } from './esm';
// Import a user's custom provider
const provider = await importModule('./my-provider.ts', 'default');
// Get Python wrapper script directory
const pythonDir = getWrapperDir('python');
// e.g., "/path/to/promptfoo/dist/src/python"