Skip to content

Commit 1d5aa55

Browse files
Matt-Dionisclaude
andcommitted
Fix ESM module loading for Node.js 18 compatibility
- Replace Function constructor with eval for better Node 18 compatibility - Add proper error handling with helpful messages - Escape single quotes to prevent injection - This fixes CI failures with ora package in Node.js 18 environment 🤖 Generated with Claude Code Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 6d99c80 commit 1d5aa55

1 file changed

Lines changed: 12 additions & 4 deletions

File tree

claude-config-composer/src/utils/esm-loader.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,16 @@
33
* This works around TypeScript's transformation of dynamic imports
44
*/
55
export async function loadEsmModule<T = any>(moduleName: string): Promise<T> {
6-
// Use Function constructor to prevent TypeScript from transforming the import
7-
const dynamicImport = new Function('moduleName', 'return import(moduleName)');
8-
const module = await dynamicImport(moduleName);
9-
return module.default || module;
6+
try {
7+
// Use eval to preserve the import() expression
8+
// This prevents TypeScript/bundlers from transforming it
9+
// We escape single quotes to prevent injection
10+
const module = await eval(`import('${moduleName.replace(/'/g, "\\'")}')`);
11+
return module.default || module;
12+
} catch (error: any) {
13+
// If import fails, provide helpful error message
14+
throw new Error(
15+
`Failed to load ESM module '${moduleName}': ${error?.message || 'Unknown error'}`
16+
);
17+
}
1018
}

0 commit comments

Comments
 (0)