Description
The TypeScript SDK (mem0ai/oss) fails to load when the ollama package is not installed, even when using a completely different LLM provider (e.g., Gemini).
Environment
- mem0ai version: 2.1.38
- Node.js: 18+
- OS: Ubuntu 22.04
Configuration
import { Memory } from "mem0ai/oss";
const config = {
llm: {
provider: "gemini", // NOT using Ollama
config: {
model: "gemini-2.0-flash-001",
apiKey: process.env.GEMINI_API_KEY,
}
},
embedder: {
provider: "gemini", // NOT using Ollama
config: {
model: "gemini-embedding-001",
apiKey: process.env.GEMINI_API_KEY,
}
},
vectorStore: {
provider: "redis",
config: { redisUrl: "redis://localhost:6379" }
}
};
const memory = new Memory(config);
Error
Error: Cannot find module 'ollama'
Require stack:
- /app/node_modules/mem0ai/dist/oss/index.js
Root Cause
The bundled dist/oss/index.js contains static requires for ALL providers:
var import_ollama = require("ollama"); // Line executed at module load
var import_ollama2 = require("ollama"); // Even if not using Ollama!
Expected Behavior
Peer dependencies like ollama should be dynamically imported only when the corresponding provider is actually used:
// Instead of:
const ollama = require("ollama");
// Should be:
async function getOllamaClient() {
const { Ollama } = await import("ollama");
return new Ollama();
}
Workaround
Currently users must install ollama even if not using it:
Comparison with Python SDK
The Python SDK handles this correctly with lazy imports and clear error messages:
# mem0/llms/ollama.py
try:
from ollama import Client
except ImportError:
raise ImportError("The 'ollama' library is required...")
Suggested Fix
- Use dynamic
import() for optional provider dependencies
- Or split the bundle so provider-specific code is in separate chunks
- Or use try/catch around requires with graceful fallback
Related
Description
The TypeScript SDK (
mem0ai/oss) fails to load when theollamapackage is not installed, even when using a completely different LLM provider (e.g., Gemini).Environment
Configuration
Error
Root Cause
The bundled
dist/oss/index.jscontains static requires for ALL providers:Expected Behavior
Peer dependencies like
ollamashould be dynamically imported only when the corresponding provider is actually used:Workaround
Currently users must install
ollamaeven if not using it:Comparison with Python SDK
The Python SDK handles this correctly with lazy imports and clear error messages:
Suggested Fix
import()for optional provider dependenciesRelated