thedotmack/claude-mem · error · Error
Corrupt JSON file, refusing to overwrite
Error message
Corrupt JSON file, refusing to overwrite: ${filePath}: ${error instanceof Error ? error.message : String(error)} What it means
readJsonSafe reads a JSON file and parses it (with BOM tolerance for Windows tooling); if reading or parsing fails, it throws with the file path and underlying message, explicitly refusing to let callers overwrite a file it could not understand. The intent is data protection: an unparseable config file may still hold user edits, so blindly overwriting it would destroy data.
Solutions
- Open the file, fix the JSON syntax error (or the empty/truncated content), and save
- Back up the corrupt file, then restore it from a known-good version and rerun
- If truly unrecoverable, manually move/delete the file so a fresh valid one can be written — after confirming nothing valuable is lost
- Re-save the file as UTF-8 without BOM if Windows tooling rewrote it
Example fix
// before
const config = readJsonSafe<Config>(path); // throws on corrupt file
// after
let config: Config;
try {
config = readJsonSafe<Config>(path);
} catch {
backupCorruptFile(path);
config = defaultConfig;
} Defensive patterns
Strategy: try-catch
Validate before calling
import { existsSync, readFileSync } from 'fs';
if (existsSync(filePath)) { try { JSON.parse(readFileSync(filePath, 'utf-8').replace(/^\uFEFF/, '')); } catch (e) { console.error('corrupt JSON:', filePath, e.message); } } Try / catch
try { const cfg = readJsonSafe<Config>(path); }
catch (e) { if (e.message.startsWith('Corrupt JSON file')) { backupAndReset(path); const cfg = readJsonSafe<Config>(path); } else throw e; } Prevention
- Write JSON atomically (temp file + rename) to avoid truncated files
- Validate JSON after manual edits with a linter/formatter
- Save config files as UTF-8 without BOM when using Windows editors
- Back up config files before uninstall/reinstall operations
When it happens
Trigger: Calling any consumer (knownMarketplaces, installedPlugins, settings, readMcpConfigTolerantly, etc.) when the target file contains invalid JSON (truncated write, manual edit typo, BOM handled elsewhere, empty file).
Common situations: A crashed process left a half-written settings.json; user hand-edited JSON and introduced a syntax error; PowerShell 5.1 or an editor rewrote the file with a UTF-8 BOM (#3013); file exists but is empty.
Understand the failure class
Background: JSON parse error: "Unexpected token" / "not valid JSON" / "failed to parse" — what JSON parsers are really complaining about — this error's family across 45 libraries.
Related errors
- CLAUDE_MEM_TELEGRAM_WRAPUP_ROUTES must be a JSON object
- ActiveServerQueueManager requires…
- Anthropic API error
- canonical content
- canonical content
AI-assisted analysis of thedotmack/claude-mem@d8bc9755e7 (2026-09-17).
Data as JSON: /api/errors/6620b2666e93de6d.
Report an issue: GitHub.
Appendix: source
Thrown at src/utils/json-utils.ts:13
import { existsSync, readFileSync } from 'fs';
import { parseJsonWithBom } from '../shared/atomic-json.js';
export function readJsonSafe<T>(filePath: string, defaultValue: T): T {
if (!existsSync(filePath)) return defaultValue;
try {
// Windows tooling (PowerShell 5.1, some editors) may rewrite JSON with a
// UTF-8 BOM. Node/Bun decode that to U+FEFF at offset 0, which JSON.parse
// rejects. Strip on read so install/uninstall and other callers survive.
// See #3013.
return parseJsonWithBom<T>(readFileSync(filePath, 'utf-8'));
} catch (error: unknown) {
throw new Error(`Corrupt JSON file, refusing to overwrite: ${filePath}: ${error instanceof Error ? error.message : String(error)}`);
}
}
View on GitHub (pinned to d8bc9755e7)