Back to Ruflo

NPX Memory Command Fix - v2.7.19 Final Solution

v2/docs/NPX_MEMORY_FIX_v2.7.19.md

3.6.308.5 KB
Original Source

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:

bash
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

  1. npx installs packages in temporary directories (/home/user/.npm/_npx/{hash}/)
  2. Optional dependencies are not installed in npx temp directories
  3. better-sqlite3 is an optional dependency required by ReasoningBank
  4. ReasoningBank initialization fails when better-sqlite3 is missing
  5. 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 return false instead of throwing
  • Problem: detectMemoryMode() didn't check return value, still tried to use ReasoningBank

✅ v2.7.19 (WORKING SOLUTION)

What was done:

  1. ensureInitialized() returns false when initialization fails in npx (instead of throwing)
  2. detectMemoryMode() checks if initialization returned false and falls back to JSON
  3. Shows helpful error message with 3 solutions
  4. 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

javascript
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

javascript
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)

bash
$ 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)

bash
$ 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

bash
$ npx claude-flow@alpha --version
v2.7.19
✅ PASS

Test 2: Memory Store (npx)

bash
$ 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)

bash
$ npx claude-flow@alpha memory query "test"
✅ Found 1 result(s):
   test-key = test-value (namespace: default)
✅ PASS

Test 4: Memory Stats (npx)

bash
$ npx claude-flow@alpha memory stats
✅ Memory Bank Statistics:
   Total Entries: 1
   Namespaces: 1
   Size: 10 bytes
✅ PASS

Files Changed

  1. src/reasoningbank/reasoningbank-adapter.js

    • Modified ensureInitialized() to return false instead of throwing
    • Added npx detection and helpful error messages
    • Lines changed: 36-76
  2. src/cli/simple-commands/memory.js

    • Modified detectMemoryMode() to check initialization return value
    • Added automatic fallback logic
    • Lines changed: 418-457
  3. package.json

    • Version: 2.7.16 → 2.7.19
  4. 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

  1. npx limitation is real: Optional dependencies never install in temp directories
  2. Silent failures are better than crashes: Return false, don't throw
  3. Check all return values: Even if initialization "succeeds", verify the result
  4. Graceful degradation: JSON fallback works fine for most use cases
  5. Clear error messages: Tell users exactly what happened and how to fix it

For Quick Testing (npx)

bash
npx claude-flow@alpha memory store "key" "value"
# Works with JSON fallback - no installation required

For Production (local install)

bash
npm install claude-flow@alpha
node_modules/.bin/claude-flow memory store "key" "value"
# Uses SQLite with full ReasoningBank features

For Workflows (MCP tools)

javascript
// 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!