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:
co-authored by
Claude Opus 4.5
parent
1c8144ba08
commit
c87c5487aa
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@colbymchenry/codegraph",
|
"name": "@colbymchenry/codegraph",
|
||||||
"version": "0.2.1",
|
"version": "0.2.2",
|
||||||
"description": "A local-first code intelligence system that builds a semantic knowledge graph from any codebase",
|
"description": "A local-first code intelligence system that builds a semantic knowledge graph from any codebase",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
"types": "dist/index.d.ts",
|
"types": "dist/index.d.ts",
|
||||||
|
|||||||
+15
-2
@@ -5,12 +5,22 @@
|
|||||||
* Uses ONNX runtime under the hood for fast local inference.
|
* Uses ONNX runtime under the hood for fast local inference.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { pipeline, env } from '@xenova/transformers';
|
|
||||||
import * as path from 'path';
|
import * as path from 'path';
|
||||||
import * as fs from 'fs';
|
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 for the feature extraction pipeline
|
||||||
type FeatureExtractionPipeline = Awaited<ReturnType<typeof pipeline<'feature-extraction'>>>;
|
type FeatureExtractionPipeline = any;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Default model for embeddings
|
* Default model for embeddings
|
||||||
@@ -93,6 +103,9 @@ export class TextEmbedder {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Load transformers.js dynamically (ESM-only package)
|
||||||
|
const { pipeline, env } = await getTransformers();
|
||||||
|
|
||||||
// Configure transformers.js to use local cache
|
// Configure transformers.js to use local cache
|
||||||
env.cacheDir = this.cacheDir;
|
env.cacheDir = this.cacheDir;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user