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(utils): load SAX parser from CommonJS default export (#4186)

XuJian5d55386965de
diff --git a/packages/utils/src/internals/sitemap.ts b/packages/utils/src/internals/sitemap.ts
index 29aa5bce2..7bf52c64d 100644
--- a/packages/utils/src/internals/sitemap.ts
+++ b/packages/utils/src/internals/sitemap.ts
@@ -87,7 +87,7 @@ class SitemapXmlParser extends Transform {
#url: Partial<SitemapUrl> = {};
static async create(): Promise<SitemapXmlParser> {
- const { SAXParser } = await import('sax');
+ const { SAXParser } = (await import('sax')).default;
return new SitemapXmlParser(new SAXParser(true));
}
diff --git a/packages/utils/test/sitemap-esm.test.ts b/packages/utils/test/sitemap-esm.test.ts
new file mode 100644
index 000000000..0b20ce2ab
--- /dev/null
+++ b/packages/utils/test/sitemap-esm.test.ts
@@ -0,0 +1,29 @@
+import { execFile } from 'node:child_process';
+import { resolve } from 'node:path';
+import { promisify } from 'node:util';
+
+import { expect, it } from 'vitest';
+
+it('parses XML sitemaps with the native Node ESM loader', async () => {
+ // Vitest synthesizes CJS named exports; a child process loads `sax` the way Node does for users.
+ const root = resolve(__dirname, '../../..');
+ const { stdout } = await promisify(execFile)(
+ process.execPath,
+ [
+ '--import',
+ 'tsx',
+ '--input-type=module',
+ '--eval',
+ `
+ import { Sitemap } from './packages/utils/src/index.ts';
+ const sitemap = await Sitemap.fromXmlString(
+ '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"><url><loc>https://example.com/catalog?item=1&amp;lang=en</loc></url></urlset>',
+ );
+ process.stdout.write(JSON.stringify(sitemap.urls));
+ `,
+ ],
+ { cwd: root },
+ );
+
+ expect(JSON.parse(stdout)).toEqual(['https://example.com/catalog?item=1&lang=en']);
+});
diff --git a/test/otel/hook-delivery.test.ts b/test/otel/hook-delivery.test.ts
index 885f07721..7e4d856e4 100644
--- a/test/otel/hook-delivery.test.ts
+++ b/test/otel/hook-delivery.test.ts
@@ -42,8 +42,16 @@ describe('module hook delivery', () => {
const output = join(outputDir, 'spans.json');
await promisify(execFile)(
- join(root, 'node_modules/.bin/tsx'),
- ['--import', fixture('register-hook.ts'), '--import', fixture('otel-setup.ts'), fixture('crawler.ts')],
+ process.execPath,
+ [
+ '--import',
+ 'tsx',
+ '--import',
+ fixture('register-hook.ts'),
+ '--import',
+ fixture('otel-setup.ts'),
+ fixture('crawler.ts'),
+ ],
{
cwd: root,
env: {