Skip to content

[Bug]: Provider factories crash with MODULE_NOT_FOUND when imported from published npm package #27

@morozow

Description

@morozow

Package Version

1.1.1

Node.js Version

v22.12

Executor Type

InProcessExecutor

Bug Description

Context

Provider factories (openAI, anthropic, gemini) were introduced to replace verbose new OpenAIProvider(config, SDK) construction with declarative openAI({ apiKey, models }) calls. Internally, each factory uses createRequire(import.meta.url) to lazily load the external SDK peer dependency (e.g., openai). The same esmRequire pattern was also applied to load the internal provider class (../providers/OpenAIProvider.js).

Problem

When a consumer installs @stdiobus/mcp-agentic from npm and calls any provider factory, the process crashes:

Error: Cannot find module '../providers/OpenAIProvider.js'
Require stack:
- /path/to/node_modules/@stdiobus/mcp-agentic/out/dist/index.js
    at node:internal/modules/cjs/loader:1249:15
    code: 'MODULE_NOT_FOUND'

Root cause: esbuild bundles all internal modules into a single file out/dist/index.js. The createRequire(import.meta.url) call creates a require function anchored to the bundle's location on disk. When the factory calls esmRequire('../providers/OpenAIProvider.js'), it resolves to out/providers/OpenAIProvider.js — a path that does not exist because the module is inlined in the bundle.

esbuild cannot statically analyze esmRequire(...) — it is an opaque function call, not a require() or import statement. So esbuild leaves it as-is in the output. The call works in development (running from source via tsx) because the relative path resolves to the actual file. It breaks only in the published bundle.

The pattern is correct for external peer dependencies (esmRequire('openai')) because those are marked external in esbuild config and resolve from the consumer's node_modules at runtime. It is incorrect for internal modules that esbuild bundles.

Reproduction

mkdir /tmp/test-mcp && cd /tmp/test-mcp
npm init -y
npm install @stdiobus/mcp-agentic@1.1.0

# This crashes:
node -e "
const { openAI } = require('@stdiobus/mcp-agentic');
openAI({ apiKey: 'test', models: ['gpt-4o'] });
"
# Error: Cannot find module '../providers/OpenAIProvider.js'

All three factories are affected: openAI, anthropic, gemini.

Impact

  • Severity: critical. Any consumer using the Provider Factory API from the published package gets a crash. The entire Factory API (openAI, anthropic, gemini, createMultiProviderAgent with factories) is unusable from npm.
  • Workaround: Use the deprecated class-based API (new OpenAIProvider(config, SDK)) directly, which is statically imported and bundled correctly.
  • Not caught by CI because the pack e2e test only verified the CLI binary, not library imports from the installed tarball.

Expected behavior

Calling openAI({ apiKey: '...', models: ['gpt-4o'] }) from the installed npm package should either:

  • Return an AIProvider instance (when openai SDK is installed), or
  • Throw BridgeError CONFIG with "openai package is not installed" (when SDK is missing)

It should never throw MODULE_NOT_FOUND for an internal module.

Steps to Reproduce

mkdir /tmp/test-mcp && cd /tmp/test-mcp
npm init -y
npm install @stdiobus/mcp-agentic@1.1.0

# This crashes:
node -e "
const { openAI } = require('@stdiobus/mcp-agentic');
openAI({ apiKey: 'test', models: ['gpt-4o'] });
"

Expected Behavior

Calling openAI({ apiKey: '...', models: ['gpt-4o'] }) from the installed npm package should either:

  • Return an AIProvider instance (when openai SDK is installed), or
  • Throw BridgeError CONFIG with "openai package is not installed" (when SDK is missing)

Actual Behavior

Error: Cannot find module '../providers/OpenAIProvider.js'

Additional Context

No response

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions