- Reorganize v3/ into implementation/ subfolders: - planning/, architecture/, migration/, integration/ - security/, adrs/, research/, swarm-plans/ - Add 15-agent concurrent swarm implementation plans: - SWARM-OVERVIEW.md: Hierarchical mesh topology, 4 phases - AGENT-SPECIFICATIONS.md: 15 agent configs with capabilities - TDD-LONDON-SCHOOL-PLAN.md: Mock-first TDD methodology - BENCHMARK-OPTIMIZATION.md: Performance targets (2.49x-7.47x) - DEPLOYMENT-PLAN.md: Release strategy, CI/CD, rollback - GITHUB-ISSUE-TRACKING.md: Issue workflow with agent replies - Update .claude/settings.json with proper hooks format - Add bash aliases for claude --dangerously-skip-permissions 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
8.5 KiB
NPX Memory Command Fix - v2.7.19 Final Solution
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)
Problem Summary
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
Root Cause
- npx installs packages in temporary directories (
/home/user/.npm/_npx/{hash}/) - Optional dependencies are not installed in npx temp directories
- better-sqlite3 is an optional dependency required by ReasoningBank
- ReasoningBank initialization fails when better-sqlite3 is missing
- Command crashes instead of falling back gracefully
Solution Evolution
❌ v2.7.16 (First Attempt)
What was done:
- Added npx detection in
reasoningbank-adapter.js - Showed helpful error message
- Problem: Still threw error, preventing fallback
❌ v2.7.17 (Second Attempt)
What was done:
- Expanded error detection to include "could not run migrations"
- Problem: Still throwing error at ensureInitialized()
❌ v2.7.18 (Third Attempt)
What was done:
- Changed
ensureInitialized()to returnfalseinstead of throwing - Problem:
detectMemoryMode()didn't check return value, still tried to use ReasoningBank
✅ v2.7.19 (WORKING SOLUTION)
What was done:
ensureInitialized()returnsfalsewhen initialization fails in npx (instead of throwing)detectMemoryMode()checks if initialization returnedfalseand falls back to JSON- Shows helpful error message with 3 solutions
- Command completes successfully with JSON storage
Technical Implementation
File 1: src/reasoningbank/reasoningbank-adapter.js
Change: 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}`);
}
})();
File 2: src/cli/simple-commands/memory.js
Change: 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';
}
}
Expected User Experience (v2.7.19+)
With npx (Automatic Fallback)
$ 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
With Local Install (Full Features)
$ 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
Validation Tests
Test 1: Version Check
$ npx claude-flow@alpha --version
v2.7.19
✅ PASS
Test 2: Memory Store (npx)
$ npx claude-flow@alpha memory store "test-key" "test-value"
⚠️ NPX LIMITATION DETECTED
✅ Automatically using JSON fallback for this command
✅ Stored successfully
✅ PASS
Test 3: Memory Query (npx)
$ npx claude-flow@alpha memory query "test"
✅ Found 1 result(s):
test-key = test-value (namespace: default)
✅ PASS
Test 4: Memory Stats (npx)
$ npx claude-flow@alpha memory stats
✅ Memory Bank Statistics:
Total Entries: 1
Namespaces: 1
Size: 10 bytes
✅ PASS
Files Changed
-
src/reasoningbank/reasoningbank-adapter.js
- Modified
ensureInitialized()to return false instead of throwing - Added npx detection and helpful error messages
- Lines changed: 36-76
- Modified
-
src/cli/simple-commands/memory.js
- Modified
detectMemoryMode()to check initialization return value - Added automatic fallback logic
- Lines changed: 418-457
- Modified
-
package.json
- Version: 2.7.16 → 2.7.19
-
docs/MEMORY_COMMAND_FIX.md
- Updated with v2.7.19 solution
- Documented automatic fallback behavior
Why Previous Versions Failed
v2.7.16 → v2.7.17
- Error detection worked
- Error message showed
- But: Still threw error, command failed
v2.7.18
- Stopped throwing error (returned false)
- But:
detectMemoryMode()didn't check return value - Still tried to use ReasoningBank, called
storeMemory(), which failed
v2.7.19 ✅
- Returns false on failure
- AND: Checks return value before using ReasoningBank
- Falls back to JSON successfully
- Command completes without error
Key Learnings
- npx limitation is real: Optional dependencies never install in temp directories
- Silent failures are better than crashes: Return false, don't throw
- Check all return values: Even if initialization "succeeds", verify the result
- Graceful degradation: JSON fallback works fine for most use cases
- Clear error messages: Tell users exactly what happened and how to fix it
Recommended Usage
For Quick Testing (npx)
npx claude-flow@alpha memory store "key" "value"
# Works with JSON fallback - no installation required
For Production (local install)
npm install claude-flow@alpha
node_modules/.bin/claude-flow memory store "key" "value"
# Uses SQLite with full ReasoningBank features
For Workflows (MCP tools)
// 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
Summary
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!