{"record":{"id":"8c9ee1dd201fd8cd","repo":"thedotmack/claude-mem","slug":"invalid-claude-mem-worker-port-in-settings-json-m","errorCode":null,"errorMessage":"Invalid CLAUDE_MEM_WORKER_PORT in settings.json: missing","messagePattern":"Invalid CLAUDE_MEM_WORKER_PORT in settings\\.json: missing","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"scripts/export-memories.ts","lineNumber":20,"sourceCode":"\nimport { writeFileSync } from 'fs';\nimport { join } from 'path';\nimport { pathToFileURL } from 'url';\nimport { SettingsDefaultsManager } from '../src/shared/SettingsDefaultsManager.js';\nimport { resolveDataDir } from '../src/shared/paths.js';\nimport type {\n  ObservationRecord,\n  SdkSessionRecord,\n  SessionSummaryRecord,\n  UserPromptRecord,\n  ExportData\n} from './types/export.js';\n\nconst WORKER_FETCH_TIMEOUT_MS = 30_000;\n\nfunction parseWorkerPort(rawPort: unknown): number {\n  if (typeof rawPort !== 'string' || rawPort.trim() === '') {\n    throw new Error('Invalid CLAUDE_MEM_WORKER_PORT in settings.json: missing');\n  }\n\n  const normalized = rawPort.trim();\n  const port = Number.parseInt(normalized, 10);\n  if (!Number.isInteger(port) || port < 1 || port > 65535 || String(port) !== normalized) {\n    throw new Error(`Invalid CLAUDE_MEM_WORKER_PORT in settings.json: ${rawPort}`);\n  }\n  return port;\n}\n\nasync function fetchWithTimeout(url: string, init?: RequestInit): Promise<Response> {\n  const controller = new AbortController();\n  const timeout = setTimeout(() => controller.abort(), WORKER_FETCH_TIMEOUT_MS);\n\n  try {\n    return await fetch(url, {\n      ...init,\n      signal: controller.signal,","sourceCodeStart":2,"sourceCodeEnd":38,"githubUrl":"https://github.com/thedotmack/claude-mem/blob/d768ba364302d12b76e69e4f021f0bb1d2d50ed6/scripts/export-memories.ts#L2-L38","documentation":"Thrown by parseWorkerPort() in the export-memories script when the CLAUDE_MEM_WORKER_PORT setting read from ~/.claude-mem/settings.json is not a non-empty string. The export script needs this port to build the worker base URL (http://localhost:<port>), so a missing/blank value makes every downstream HTTP call impossible. The guard runs before any network activity, so this is a pure configuration precondition failure.","triggerScenarios":"Calling exportMemories() (or running `tsx scripts/export-memories.ts ...`) when settings.json has no CLAUDE_MEM_WORKER_PORT key, has it set to null/undefined, set to a number instead of a string, or set to a whitespace-only string. Also fires when SettingsDefaultsManager.loadFromFile returns an object lacking the key because the file is empty or the worker was never started.","commonSituations":"First-time use before the worker has been started (the worker normally writes its port into settings.json on boot). A settings.json that was hand-edited and the key was deleted. Running the export script on a machine where claude-mem was only partially installed. A fresh data dir (~/.claude-mem) with no settings.json yet.","solutions":["Start the claude-mem worker so it populates settings.json with CLAUDE_MEM_WORKER_PORT, then re-run the export.","Inspect ~/.claude-mem/settings.json and confirm CLAUDE_MEM_WORKER_PORT exists; if missing, run the install/server command that writes it.","If settings.json is empty or malformed, run the standard setup flow (npx claude-mem install / server start) to regenerate it.","As a last resort, manually add \"CLAUDE_MEM_WORKER_PORT\": \"<port>\" to ~/.claude-mem/settings.json matching the port the worker is actually bound to."],"exampleFix":"// before — settings.json missing the key\n{}\n// after\n{ \"CLAUDE_MEM_WORKER_PORT\": \"44328\" }","handlingStrategy":"validation","validationCode":"import { existsSync, readFileSync } from 'fs';\nconst p = join(resolveDataDir(), 'settings.json');\nif (!existsSync(p)) throw new Error('Run the worker first to populate settings.json');\nconst s = JSON.parse(readFileSync(p, 'utf-8'));\nif (typeof s.CLAUDE_MEM_WORKER_PORT !== 'string' || s.CLAUDE_MEM_WORKER_PORT.trim() === '') {\n  throw new Error('CLAUDE_MEM_WORKER_PORT missing in settings.json');\n}","typeGuard":"function isNonEmptyString(v: unknown): v is string {\n  return typeof v === 'string' && v.trim() !== '';\n}","tryCatchPattern":"try {\n  const port = parseWorkerPort(settings.CLAUDE_MEM_WORKER_PORT);\n} catch (e) {\n  if (e instanceof Error && e.message.includes('missing')) {\n    console.error('Start the worker so it writes the port, then retry.');\n    process.exit(1);\n  }\n  throw e;\n}","preventionTips":["Always start the worker before running export scripts so it writes its port to settings.json.","Treat settings.json as worker-owned; don't hand-delete the CLAUDE_MEM_WORKER_PORT key.","Validate settings.json schema in a preflight check before any worker-dependent command."],"tags":["config","validation","settings","worker","export"],"backgroundTag":null,"analyzedSha":"d768ba364302d12b76e69e4f021f0bb1d2d50ed6","analyzedAt":"2026-08-12T23:52:55.241Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}