v2/docs/NPX_MEMORY_FIX_v2.7.19.md
Issue: npx claude-flow@alpha memory commands fail with BetterSqlite3 constructor error
Status: ✅ FIXED in v2.7.19
Date: 2025-10-25
Versions: v2.7.16 → v2.7.17 → v2.7.18 → v2.7.19 (WORKING)
When users run memory commands via npx claude-flow@alpha memory store ..., they encounter:
TypeError: BetterSqlite3 is not a constructor
Migration error: TypeError: BetterSqlite3 is not a constructor
❌ Failed to store: Failed to initialize ReasoningBank: could not run migrations
/home/user/.npm/_npx/{hash}/)What was done:
reasoningbank-adapter.jsWhat was done:
What was done:
ensureInitialized() to return false instead of throwingdetectMemoryMode() didn't check return value, still tried to use ReasoningBankWhat was done:
ensureInitialized() returns false when initialization fails in npx (instead of throwing)detectMemoryMode() checks if initialization returned false and falls back to JSONsrc/reasoningbank/reasoningbank-adapter.jsChange: Return false instead of throwing error on npx failure
initPromise = (async () => {
try {
await ReasoningBank.initialize();
backendInitialized = true;
return true;
} catch (error) {
const isSqliteError = error.message?.includes('BetterSqlite3 is not a constructor') ||
error.message?.includes('better-sqlite3') ||
error.message?.includes('could not run migrations');
const isNpx = process.env.npm_config_user_agent?.includes('npx') ||
process.cwd().includes('_npx');
if (isSqliteError && isNpx) {
// Show helpful message but DON'T throw - allow fallback
console.error('\n⚠️ NPX LIMITATION DETECTED\n');
console.error('ReasoningBank requires better-sqlite3, not available in npx.\n');
console.error('📚 Solutions:\n');
console.error(' 1. LOCAL INSTALL: npm install && node_modules/.bin/claude-flow\n');
console.error(' 2. USE MCP TOOLS: mcp__claude-flow__memory_usage(...)\n');
console.error(' 3. USE JSON FALLBACK (automatic): Command will continue...\n');
return false; // Signal failure but allow fallback
}
// Other errors - throw normally
throw new Error(`Failed to initialize ReasoningBank: ${error.message}`);
}
})();
src/cli/simple-commands/memory.jsChange: Check initialization result before using ReasoningBank
async function detectMemoryMode(flags, subArgs) {
// ... [earlier code]
try {
const { initializeReasoningBank } = await import('../../reasoningbank/reasoningbank-adapter.js');
const initialized = await initializeReasoningBank();
// Check if initialization succeeded
if (!initialized) {
// Initialization failed - fall back to JSON
const isNpx = process.env.npm_config_user_agent?.includes('npx') ||
process.cwd().includes('_npx');
if (isNpx) {
console.log('\n✅ Automatically using JSON fallback for this command\n');
} else {
printWarning(`⚠️ SQLite unavailable, using JSON fallback`);
}
return 'basic'; // Use JSON mode
}
// Success - use ReasoningBank
printInfo('🗄️ Initialized SQLite backend (.swarm/memory.db)');
return 'reasoningbank';
} catch (error) {
// Handle other errors...
return 'basic';
}
}
$ npx claude-flow@alpha memory store "api-design" "REST with JWT auth"
⚠️ NPX LIMITATION DETECTED
ReasoningBank requires better-sqlite3, not available in npx temp directories.
📚 Solutions:
1. LOCAL INSTALL (Recommended):
npm install && node_modules/.bin/claude-flow memory store "key" "value"
2. USE MCP TOOLS instead:
mcp__claude-flow__memory_usage({ action: "store", key: "test", value: "data" })
3. USE JSON FALLBACK (automatic):
Command will continue with JSON storage...
✅ Automatically using JSON fallback for this command
✅ Stored successfully
📝 Key: api-design
📦 Namespace: default
💾 Size: 18 bytes
$ npm install
$ node_modules/.bin/claude-flow memory store "api-design" "REST with JWT auth"
🗄️ Initialized SQLite backend (.swarm/memory.db)
✅ Stored successfully in ReasoningBank
📝 Key: api-design
🧠 Memory ID: abc123...
📦 Namespace: default
💾 Size: 18 bytes
$ npx claude-flow@alpha --version
v2.7.19
✅ PASS
$ npx claude-flow@alpha memory store "test-key" "test-value"
⚠️ NPX LIMITATION DETECTED
✅ Automatically using JSON fallback for this command
✅ Stored successfully
✅ PASS
$ npx claude-flow@alpha memory query "test"
✅ Found 1 result(s):
test-key = test-value (namespace: default)
✅ PASS
$ npx claude-flow@alpha memory stats
✅ Memory Bank Statistics:
Total Entries: 1
Namespaces: 1
Size: 10 bytes
✅ PASS
src/reasoningbank/reasoningbank-adapter.js
ensureInitialized() to return false instead of throwingsrc/cli/simple-commands/memory.js
detectMemoryMode() to check initialization return valuepackage.json
docs/MEMORY_COMMAND_FIX.md
detectMemoryMode() didn't check return valuestoreMemory(), which failednpx claude-flow@alpha memory store "key" "value"
# Works with JSON fallback - no installation required
npm install claude-flow@alpha
node_modules/.bin/claude-flow memory store "key" "value"
# Uses SQLite with full ReasoningBank features
// In Claude Code / Claude Desktop
mcp__claude-flow__memory_usage({
action: "store",
key: "api-pattern",
value: "REST with JWT auth",
namespace: "default"
})
// Best integration, no dependency issues
Problem: npx memory commands crashed due to missing better-sqlite3 Solution: Detect npx, return false instead of throwing, check return value, fall back to JSON Result: Commands work via npx with helpful error messages and automatic JSON fallback
Status: ✅ FIXED AND VALIDATED Version: v2.7.19 Date: 2025-10-25
All npx memory commands now work correctly with automatic JSON fallback!