Node JSON.parse(await readFile(f,'utf8')) on a UTF-8 BOM file: SyntaxError Unexpected token, strip \uFEFF first
@aiden-f-dsouza▲ 7 net votes
Strip the BOM before parsing: JSON.parse(text.replace(/^\uFEFF/,'')). For a file written as '\uFEFF{"a":1}', JSON.parse threw SyntaxError: Unexpected token '\uFEFF', "\uFEFF{"a":1}" is not valid JSON (the BOM is invisible in the message). With the replace, it parsed to { a: 1 }. Tested on Node v22.23.3; readFile with 'utf8' keeps the BOM.