MCPcopy Index your code
hub / github.com/promptfoo/promptfoo / loadCjsModule

Function loadCjsModule

src/esm.ts:353–478  ·  view source on GitHub ↗
(modulePath: string)

Source from the content-addressed store, hash-verified

351
352// biome-ignore lint/suspicious/noExplicitAny: This is actually loading a user space module that can be anything
353function loadCjsModule(modulePath: string): any {
354 const code = fs.readFileSync(modulePath, 'utf-8');
355 const dirname = path.dirname(modulePath);
356 const filename = modulePath;
357
358 // Create a require function scoped to the module's directory
359 const moduleRequire = createRequire(pathToFileURL(modulePath).href);
360
361 // Create module and exports objects
362 // biome-ignore lint/suspicious/noExplicitAny: This is actually loading a user space module that can be anything
363 const moduleObj: { exports: any } = { exports: {} };
364
365 // Create a context with CJS globals
366 // We include all commonly used Node.js globals to ensure compatibility
367 const context = vm.createContext({
368 // CJS-specific globals
369 module: moduleObj,
370 exports: moduleObj.exports,
371 require: moduleRequire,
372 __dirname: dirname,
373 __filename: filename,
374
375 // Global object references (some CJS modules use these)
376 global: globalThis,
377 globalThis,
378
379 // Console and process
380 console,
381 process,
382
383 // Binary data
384 Buffer,
385
386 // Timers
387 setTimeout,
388 setInterval,
389 setImmediate,
390 clearTimeout,
391 clearInterval,
392 clearImmediate,
393 queueMicrotask,
394
395 // URL handling
396 URL,
397 URLSearchParams,
398
399 // Text encoding/decoding
400 TextEncoder,
401 TextDecoder,
402 atob: globalThis.atob,
403 btoa: globalThis.btoa,
404
405 // Fetch API (Node 18+)
406 fetch: globalThis.fetch,
407 Request: globalThis.Request,
408 Response: globalThis.Response,
409 Headers: globalThis.Headers,
410

Callers 1

importModuleFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…