From 69e2ffb436141c70ac3dee07a6303c9787a9648b Mon Sep 17 00:00:00 2001 From: Paul Date: Fri, 26 Dec 2025 14:15:27 +0530 Subject: [PATCH] fix: handle UTF-8 BOM in JSON files (#1631) Some editors (especially on Windows) add a UTF-8 BOM (byte order mark, U+FEFF) at the start of files. This invisible character causes JSON.parse to fail with 'Unexpected token' error. This fix detects the BOM and strips it from the file content before the JSON parser sees it, preventing the parse error. --- src/bin.ts | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/bin.ts b/src/bin.ts index 4633e5e43..18d8f852e 100644 --- a/src/bin.ts +++ b/src/bin.ts @@ -120,7 +120,16 @@ if (!existsSync(file)) { } // Handle empty string JSON file -if (readFileSync(file, 'utf-8').trim() === '') { +let fileContent = readFileSync(file, 'utf-8') + +// Strip UTF-8 BOM if present (U+FEFF) +// Some editors (especially on Windows) add this invisible character at the start of files +if (fileContent.charCodeAt(0) === 0xfeff) { + fileContent = fileContent.slice(1) + writeFileSync(file, fileContent) +} + +if (fileContent.trim() === '') { writeFileSync(file, '{}') }