Conversation
…g resolution The `sqlite3` package uses the `bindings` module to locate its native `.node` addon at runtime by walking up from `__dirname` of the caller. When OpenClaw loads the mem0 plugin through jiti (JIT TypeScript/ESM loader), the caller `__dirname` resolves to jiti's own location, but the actual `node_sqlite3.node` binary lives in the plugin's extension directory. The `bindings` module never searches there, causing: Error: Could not locate the bindings file. Tried: → ...jiti/build/node_sqlite3.node Replace `sqlite3` with `better-sqlite3` which ships prebuilt binaries via `prebuild-install` and uses a different native loading mechanism that does not rely on `bindings` module path walking. Changes: - Rewrite SQLiteManager to use better-sqlite3 synchronous API with cached prepared statements for addHistory/getHistory - Rewrite MemoryVectorStore to use better-sqlite3 synchronous API with transaction-wrapped batch inserts - Update tsup.config.ts external: sqlite3 → better-sqlite3 - Update peerDependencies and onlyBuiltDependencies in package.json - Update src/oss/package.json dependencies - Add @types/better-sqlite3 to devDependencies - Add 24 tests covering SQLiteManager, MemoryVectorStore, file persistence, cosine similarity search, filters, and userId mgmt Fixes #4107 Fixes #4050 Fixes #4172 Fixes #4156 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Upgrade to latest better-sqlite3 v12 for improved Node.js support (20.x, 22.x, 23.x, 24.x, 25.x). Includes prettier formatting fixes for test and vector store files. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Add v2.3.0 changelog entry for the better-sqlite3 migration, documenting the bug fixes, performance improvements, and Node 20+ requirement for OSS sqlite features. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
whysosaket
approved these changes
Mar 9, 2026
utkarsh240799
added a commit
that referenced
this pull request
Mar 10, 2026
Bump plugin version to 0.3.0 to reflect the upstream mem0ai dependency update with sqlite3 to better-sqlite3 migration (#4270). Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This was referenced Mar 10, 2026
jamebobob
pushed a commit
to jamebobob/mem0-vigil-recall
that referenced
this pull request
Mar 29, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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
sqlite3withbetter-sqlite3in bothSQLiteManager(history store) andMemoryVectorStore(in-memory vector store) to fix native binding resolution failures under OpenClaw's jiti loadertsup.config.ts,package.json,src/oss/package.json) so the package builds cleanlyProblem
The
sqlite3package uses thebindingsmodule to locate its native.nodeaddon at runtime by walking the V8 call stack to find the caller's__dirname. When OpenClaw loads the mem0 plugin through jiti (JIT TypeScript/ESM loader), V8 stack frames have synthetic filenames from jiti's transpiler, causingbindingsto resolve the wrongmodule_root:The actual binary exists in the plugin's extension directory but
bindingsnever searches there. This is architecture-independent — it affects x86_64, ARM64, macOS, and Linux equally.Additionally, the published
dist/oss/index.mjshad two unconditional top-levelimport sqlite3 from "sqlite3"statements (fromSQLiteManagerandMemoryVectorStore), so sqlite3 was evaluated eagerly at ESM parse time with no way to conditionally skip it.Solution
Replace
sqlite3withbetter-sqlite3which:prebuild-install(no compile step needed)bindingsmodule path walkingChanges
SQLiteManager.tsbetter-sqlite3's synchronous APIstmtInsert,stmtSelect) for performanceinit()is now synchronous — no more.catch(console.error)race conditionundefinedparams explicitly coerced tonullfor better-sqlite3 compatibilityMemoryVectorStore(memory.ts)better-sqlite3's synchronous APIrun(),all(),getOne()async Promise wrappersinsert()wrapped in a transaction for atomicity and performancebetter-sqlite3'sUint8Arrayreturn typetsup.config.tssqlite3→better-sqlite3package.jsonsqlite3: 5.1.7→better-sqlite3: ^11.9.1sqlite3→better-sqlite3@types/better-sqlite3: ^7.6.13@types/sqlite3from peerDependenciessrc/oss/package.jsonsqlite3→better-sqlite3in dependencies@types/sqlite3Tests (new:
better-sqlite3-migration.test.ts)24 tests covering:
Migration
Users of the
mem0ainpm package who use the OSS mode with sqlite history will need to:npm uninstall sqlite3/pnpm remove sqlite3npm install better-sqlite3/pnpm add better-sqlite3No code changes required — the API is unchanged.
Test plan
npx jest --testPathPattern=better-sqlite3-migration)npx tsc --noEmit— zero new errors)tsupbuild succeeds (ESM, CJS, DTS all generated cleanly)better-sqlite3correctly externalized in build output (verified indist/oss/index.mjs)OpenClaw End-to-End Test Results
Tested the full mem0 OSS plugin lifecycle through OpenClaw's gateway (jiti-based TypeScript plugin loader) to verify the
better-sqlite3migration works in the exact environment that triggered the originalsqlite3binding failures.Setup
/usr/local/bin/node)memoryvector store (better-sqlite3 backed), OpenAI embedder (text-embedding-3-small), OpenAI LLM (gpt-4o)historyDbPathconfig propagation bug is a separate issue — see PR fix(openclaw): propagate historyDbPath into historyStore to prevent SQLITE_CANTOPEN crash loop #4160)Results
openclaw-mem0: registered (mode: open-source, user: utkarsh, graph: false, autoRecall: true, autoCapture: true)openclaw-mem0: initialized— nosqlite3binding errors, nobindingsmodule path resolution failuresopenclaw-mem0: auto-captured 2 memories— sent "my favorite programming language is Rust and I prefer dark mode in all my editors", 2 memories extracted and storedmem0-vectors-test.dbcreated (40KB), data persisted across gateway restartsbetter-sqlite3errors after correct setup (no binding failures, no module resolution errors)Fixes
Related