fix(VAP-12030): use dynamic import for ESM-only @xenova/transformers #14
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
Fixes the
ERR_REQUIRE_ESMerror reported by customers when using the@vapi-ai/mcp-docs-servernpm package. The@xenova/transformerspackage is ESM-only, but TypeScript compiled the staticimporttorequire()in CJS output, causing a runtime crash.docs-serverTypeScript source from the published npm package into this repo (the original source repoVapiAI/mcp-docs-serveris private/inaccessible)import { pipeline } from '@xenova/transformers'(static, compiled torequire()) toconst { pipeline } = await import('@xenova/transformers')(dynamic, works in both CJS and ESM)Root Cause
@xenova/transformersis an ESM-only package. Thedocs-serverhas no"type": "module"inpackage.jsonand uses"module": "Node16"in tsconfig, so TypeScript compiles static imports torequire(). This causesERR_REQUIRE_ESMat runtime.Fix
Use dynamic
import()inside the_initialize()method instead of a static top-level import. Dynamicimport()works in both CJS and ESM contexts and is preserved by TypeScript's compilation.Key file:
docs-server/src/utils/vector-search.tsline 50Test Results
All 8 tests pass:
require('@xenova/transformers')import('@xenova/transformers')isReady(),getIndexSize(),search(),invalidateIndex()all work correctlyNotes
npm run build) has pre-existing failures onmain(missing@types/node,@types/jest) unrelated to this changedocs-server/subdirectory builds and tests independently with its owntsconfig.jsonandpackage.jsonLinear: VAP-12030