Skip to content

fix: replace sqlite3 with better-sqlite3 to fix native binding resolution#4123

Closed
worldofgeese wants to merge 1 commit intomem0ai:mainfrom
worldofgeese:fix/better-sqlite3
Closed

fix: replace sqlite3 with better-sqlite3 to fix native binding resolution#4123
worldofgeese wants to merge 1 commit intomem0ai:mainfrom
worldofgeese:fix/better-sqlite3

Conversation

@worldofgeese
Copy link
Copy Markdown

Summary

Replace sqlite3 with better-sqlite3 to fix the native binding resolution issue that occurs when the @mem0/openclaw-mem0 plugin is loaded through jiti (JIT TypeScript/ESM loader).

Problem

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, the caller __dirname resolves to jiti's own location inside the pnpm store (/app/node_modules/.pnpm/jiti@2.6.1/node_modules/jiti/), but the actual node_sqlite3.node binary lives in the plugin's extension directory (/home/node/.openclaw/extensions/openclaw-mem0/node_modules/sqlite3/build/Release/node_sqlite3.node). The bindings module never searches there.

This causes the error:

Error: Could not locate the bindings file. Tried:
 → /app/node_modules/.pnpm/jiti@2.6.1/node_modules/jiti/build/node_sqlite3.node
 → ...

Solution

Replace sqlite3 with better-sqlite3 which:

  • Ships prebuilt binaries via prebuild-install (no compile step needed)
  • Uses a different native loading mechanism that doesn't rely on the bindings module's path walking
  • Is synchronous (simpler API, often faster for the write-ahead-log pattern mem0 uses)
  • Has broader platform coverage out of the box (x64, ARM64, musl/glibc)

Changes

SQLiteManager.ts

  • Rewritten to use better-sqlite3's synchronous API
  • Removed async wrapper methods (run, all) since better-sqlite3 is sync
  • Simplified init() - no longer needs async error handling
  • addHistory, getHistory, reset now use prepared statements

package.json

  • peerDependencies: sqlite3: 5.1.7better-sqlite3: ^11.9.1
  • pnpm.onlyBuiltDependencies: sqlite3better-sqlite3
  • Removed @types/sqlite3 from peerDependencies (better-sqlite3 ships its own types)

Migration

Users will need to:

  1. npm uninstall sqlite3 or pnpm remove sqlite3
  2. npm install better-sqlite3 or pnpm add better-sqlite3

Testing

  • Test with OpenClaw plugin loading
  • Test on x86_64 Linux
  • Test on ARM64 (if available)
  • Verify all SQLiteManager methods work correctly

Fixes

Related

…tion

Replace sqlite3 with better-sqlite3 in SQLiteManager.ts to fix the
native binding resolution issue in OpenClaw plugin (mem0ai#4107).

better-sqlite3:
- Ships prebuilt binaries via prebuild-install (no compile step)
- Uses different native loading mechanism that works with jiti bundler
- Is synchronous (simpler API, often faster for WAL pattern)
- Has broader platform coverage (x64, ARM64, musl/glibc)

Changes:
- Rewrite SQLiteManager to use better-sqlite3 synchronous API
- Update peerDependencies: sqlite3 -> better-sqlite3 ^11.9.1
- Update onlyBuiltDependencies: sqlite3 -> better-sqlite3
- Remove @types/sqlite3 (better-sqlite3 ships its own types)

Fixes mem0ai#4107
Fixes mem0ai#4050 (same root cause)
@CLAassistant
Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.


Tao Hansen seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account.
You have signed the CLA already but the status is still pending? Let us recheck it.

@utkarsh240799
Copy link
Copy Markdown
Contributor

The issue was resolved in #4270

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[@mem0/openclaw-mem0] sqlite3 native binding resolution fails due to jiti path mismatch [OpenClaw Plugin] ARM64 sqlite3 binding error on Node v22

3 participants