Skip to content

Crawlee—A web scraping and browser automation library for Node.js to build reliable crawlers. In JavaScript and TypeScript. Extract data for AI, LLMs, RAG, or GPTs. Download HTML, PDF, JPG, PNG, and other files from websites. Works with Puppeteer, Playwright, Cheerio, JSDOM, and raw HTTP. Both headful and headless mode. With proxy rotation.

fix(core): read crawlee.json saved with a UTF-8 BOM (#4307)

Wei-Lun Li2369a314e619
diff --git a/packages/core/src/configuration.ts b/packages/core/src/configuration.ts
index 911dd0ffe..35561503e 100644
--- a/packages/core/src/configuration.ts
+++ b/packages/core/src/configuration.ts
@@ -1,6 +1,7 @@
import { readFileSync } from 'node:fs';
import { join } from 'node:path';
+import JSON5 from 'json5';
import { z } from 'zod';
import { log, LogLevel } from './log.js';
@@ -275,8 +276,9 @@ export class Configuration {
*/
static #loadFileOptions(): Record<string, unknown> {
try {
- const file = readFileSync(join(process.cwd(), 'crawlee.json'));
- return JSON.parse(file.toString());
+ const file = readFileSync(join(process.cwd(), 'crawlee.json'), 'utf8');
+ // JSON5 also handles a leading UTF-8 byte order mark, which some Windows tools write
+ return JSON5.parse(file);
} catch {
return {};
}
diff --git a/test/core/configuration.test.ts b/test/core/configuration.test.ts
index 078258de8..16cfda337 100644
--- a/test/core/configuration.test.ts
+++ b/test/core/configuration.test.ts
@@ -281,6 +281,15 @@ describe('Configuration', () => {
expect(typeof config.memoryMbytes).toBe('number');
});
+ it('loads crawlee.json saved with a UTF-8 byte order mark', () => {
+ // e.g. written by PowerShell 5.1 `Set-Content -Encoding UTF8` or "UTF-8 with BOM" in an editor
+ writeFileSync(crawleeJsonPath, `\uFEFF${JSON.stringify({ chromeExecutablePath: 'from-file' })}`);
+ fileCreated = true;
+
+ const config = new Configuration();
+ expect(config.chromeExecutablePath).toBe('from-file');
+ });
+
it('handles missing crawlee.json gracefully', () => {
// No file created — should fall through to defaults
const config = new Configuration();