{"record":{"id":"5b2b77e4dceae201","repo":"thedotmack/claude-mem","slug":"install-failed-to-parse-existing-settings-json","errorCode":null,"errorMessage":"[install] Failed to parse existing settings.json, starting from empty:","messagePattern":"\\[install\\] Failed to parse existing settings\\.json, starting from empty:","errorType":"console","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"src/npx-cli/commands/install.ts","lineNumber":854,"sourceCode":"    // apiKeyHelper, model, statusLine, etc.). readFlatSettings unwraps the\n    // env subtree for reads, but writing that flattened view back as the\n    // entire file silently drops every non-env top-level key — destroying\n    // user configuration that disableClaudeAutoMemory + writeJsonFileAtomic\n    // had carefully written.\n    //\n    // Track whether the file uses the env-nested shape so we mutate only the\n    // relevant subtree and preserve every other top-level key on write.\n    let document: Record<string, unknown> = {};\n    let envNested = false;\n    if (existsSync(path)) {\n      try {\n        const parsed = parseJsonWithBom(readFileSync(path, 'utf-8'));\n        if (parsed && typeof parsed === 'object') {\n          document = parsed as Record<string, unknown>;\n          envNested = typeof document.env === 'object' && document.env !== null;\n        }\n      } catch (parseError: unknown) {\n        console.warn('[install] Failed to parse existing settings.json, starting from empty:', parseError instanceof Error ? parseError.message : String(parseError));\n        document = {};\n      }\n    } else {\n      const dir = dirname(path);\n      if (!existsSync(dir)) {\n        mkdirSync(dir, { recursive: true });\n      }\n    }\n\n    const target = envNested\n      ? (document.env as Record<string, unknown>)\n      : document;\n    for (const [key, value] of Object.entries(updates)) {\n      target[key] = value;\n    }\n\n    writeSettingsJsonAtomic(path, document);\n    // settings.json can carry tokens (CMEM Pro setup token, provider API","sourceCodeStart":836,"sourceCodeEnd":872,"githubUrl":"https://github.com/thedotmack/claude-mem/blob/d8bc9755e74915e5c3b999181e10a67c889bce2a/src/npx-cli/commands/install.ts#L836-L872","documentation":"When writing Claude settings.json, the installer attempts to merge into the existing file. If the existing file cannot be parsed (corrupt JSON, BOM mishandling by older writers, truncated file), it warns and resets the document to an empty object, so existing settings in that file will be overwritten rather than merged.","triggerScenarios":"`parseJsonWithBom(readFileSync(path, 'utf-8'))` throws for the settings.json at the target path — malformed JSON, encoding issues, or a partially written file from a crashed previous install.","commonSituations":"A previous install/update crashed mid-write leaving truncated JSON; manual hand-editing of settings.json introduced a syntax error; file saved with odd encoding/BOM by an editor.","solutions":["Back up the file, fix or regenerate settings.json (validate it with `jq . settings.json`), then rerun the installer.","If the settings are disposable, delete settings.json and let the installer recreate it from scratch.","Restore settings.json from version control or a backup before reinstalling."],"exampleFix":"// validate and repair before install\njq . ~/.claude/settings.json || cp ~/.claude/settings.json.bak ~/.claude/settings.json","handlingStrategy":"try-catch","validationCode":"try {\n  const parsed = JSON.parse(fs.readFileSync(settingsPath, 'utf-8').replace(/^\\uFEFF/, ''));\n  console.log('settings.json is valid JSON');\n} catch (e) {\n  console.warn('settings.json is corrupt; back it up before installing:', e.message);\n}","typeGuard":"function isSettingsObject(v: unknown): v is Record<string, unknown> {\n  return typeof v === 'object' && v !== null && !Array.isArray(v);\n}","tryCatchPattern":"try {\n  document = parseJsonWithBom(readFileSync(path, 'utf-8'));\n} catch (parseError) {\n  console.warn('Failed to parse existing settings.json, starting from empty:', parseError.message);\n  document = {}; // back up the corrupt file first!\n}","preventionTips":["Always back up settings.json before running install/update.","Validate settings.json with `jq .` after any manual edit.","Avoid killing the installer/worker mid-write; use editors that preserve UTF-8 without stray BOM."],"tags":["json","config-file","install","data-loss-risk"],"backgroundTag":"json-parse-error","analyzedSha":"d8bc9755e74915e5c3b999181e10a67c889bce2a","analyzedAt":"2026-09-17T16:40:26.182Z","contentChangedAt":"2026-09-17T16:40:26.182Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}