Fix ESM import error for @xenova/transformers

Change static import to dynamic import() for @xenova/transformers
which is an ESM-only package. This fixes ERR_REQUIRE_ESM when
running via npx in CommonJS environments.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
Colby McHenry
2026-01-19 15:11:40 -06:00
co-authored by Claude Opus 4.5
parent 1c8144ba08
commit c87c5487aa
2 changed files with 16 additions and 3 deletions
+15 -2
View File
@@ -5,12 +5,22 @@
* Uses ONNX runtime under the hood for fast local inference.
*/
import { pipeline, env } from '@xenova/transformers';
import * as path from 'path';
import * as fs from 'fs';
// Dynamic import for @xenova/transformers (ESM-only package)
// We use dynamic import to support CommonJS builds
let transformersModule: typeof import('@xenova/transformers') | null = null;
async function getTransformers() {
if (!transformersModule) {
transformersModule = await import('@xenova/transformers');
}
return transformersModule;
}
// Type for the feature extraction pipeline
type FeatureExtractionPipeline = Awaited<ReturnType<typeof pipeline<'feature-extraction'>>>;
type FeatureExtractionPipeline = any;
/**
* Default model for embeddings
@@ -93,6 +103,9 @@ export class TextEmbedder {
return;
}
// Load transformers.js dynamically (ESM-only package)
const { pipeline, env } = await getTransformers();
// Configure transformers.js to use local cache
env.cacheDir = this.cacheDir;