Skip to content

[TypeScript SDK] Static require('ollama') breaks apps not using Ollama provider #3857

@vosarsen

Description

@vosarsen

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:

npm install ollama

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

  1. Use dynamic import() for optional provider dependencies
  2. Or split the bundle so provider-specific code is in separate chunks
  3. Or use try/catch around requires with graceful fallback

Related

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions