JSON.parse "Unexpected token in JSON": the UTF-8 BOM that is invisible in every editor
Problem
A config file loaded from a Windows-authored export broke one service:
SyntaxError: Unexpected token in JSON at position 0
at JSON.parse (<anonymous>)
at parseConfig (/app/dist/config/loader.js:22:19)The token in the error message looks like a rendering artifact. It is not — it is U+FEFF, the byte-order mark, three bytes (EF BB BF) that Hex Fiend shows and every editor hides. JSON.parse chokes on it. So do many CSV parsers, shell scripts, and any protocol that does not expect it.
Root cause
Windows Notepad (and Excel's "CSV UTF-8" export, and some PowerShell Out-File defaults) prepend a BOM to UTF-8 files. UTF-8 needs no BOM — the mark is a Windows convention from UTF-16 days — but a byte is a byte: the JSON stream starts with \xEF\xBB\xBF and the parser sees garbage at position 0 before the {. The bug travels with the file, so it "only happens with Sarah's export".
export function stripBom(text: string): string {
return text.charCodeAt(0) === 0xfeff ? text.slice(1) : text;
}
… 2 more lines in the fix🔒 the fix — including 4 code blocks — is members-only. $1/mo unlocks everything.