You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Playwright+TypeScript集成Lighthouse遇ERR_REQUIRE_ESM错误求助

解决Playwright + Lighthouse ESM兼容错误

问题场景

Windows 11 + Node.js 18环境下,基于npm init playwright@latest生成的TypeScript项目,集成playwright-lighthouse包运行Lighthouse测试时,执行npx playwright test触发ESM模块兼容错误。

核心错误信息

Error [ERR_REQUIRE_ESM]: require() of ES Module C:\pw-lhc-tests\node_modules\lighthouse\core\index.js from C:\pw-lhc-tests\node_modules\playwright-lighthouse\src\task.js not supported.
Instead change the require of index.js in C:\pw-lhc-tests\node_modules\playwright-lighthouse\src\task.js to a dynamic import() which is available in all CommonJS modules.

项目配置(package.json)

{
  "name": "pw-lhc-tests",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {},
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "@playwright/test": "^1.30.0",
    "lighthouse": "^10.0.1",
    "playwright-lighthouse": "^2.2.2"
  }
}

测试代码(example.spec.ts)

import { test } from '@playwright/test';
import { playAudit } from 'playwright-lighthouse';

test('has title', async ({ playwright }) => {
  
  const browser = await playwright['chromium'].launch({
    args: ['--remote-debugging-port=9222'],
  });
  
  const page = await browser.newPage();
  await page.goto('https://angular.io/');

  await playAudit({
    page: page,
    port: 9222,
  });

  await browser.close();
});

完整错误堆栈

Error [ERR_REQUIRE_ESM]: require() of ES Module C:\pw-lhc-tests\node_modules\lighthouse\core\index.js from C:\pw-lhc-tests\node_modules\playwright-lighthouse\src\task.js not supported.
Instead change the require of index.js in C:\pw-lhc-tests\node_modules\playwright-lighthouse\src\task.js to a dynamic import() which is available in all CommonJS modules.
   at example.spec.ts:4

  2 | import { playAudit } from 'playwright-lighthouse';
  3 |
> 4 | test('has title', async ({playwright}) => {
    |                             ^
  5 |
  6 |   const browser = await playwright['chromium'].launch({
  7 |     args: ['--remote-debugging-port=9222'],
 
 at t.<computed>.tu._extensions.<computed> [as .js] (C:\pw-lhc-tests\node_modules\@playwright\test\lib\utilsBundleImpl.js:16:1010)
    at Object.<anonymous> (C:\pw-lhc-tests\node_modules\playwright-lighthouse\src\task.js:2:23)
    at t.<computed>.tu._extensions.<computed> [as .js] (C:\pw-lhc-tests\node_modules\@playwright\test\lib\utilsBundleImpl.js:16:1010)
    at Object.<anonymous> (C:\pw-lhc-tests\node_modules\playwright-lighthouse\src\audit.js:1:24)
    at t.<computed>.tu._extensions.<computed> [as .js] (C:\pw-lhc-tests\node_modules\@playwright\test\lib\utilsBundleImpl.js:16:1010)
    at Object.<anonymous> (C:\pw-lhc-tests\node_modules\playwright-lighthouse\index.js:1:23)
    at t.<computed>.tu._extensions.<computed> [as .js] (C:\pw-lhc-tests\node_modules\@playwright\test\lib\utilsBundleImpl.js:16:1010)
    at Object.<anonymous> (C:\pw-lhc-tests\tests\example.spec.ts:4:29)
    at A._compile (C:\pw-lhc-tests\node_modules\@playwright\test\lib\utilsBundleImpl.js:16:994)
    at t.<computed>.tu._extensions.<computed> [as .ts] (C:\pw-lhc-tests\node_modules\@playwright\test\lib\utilsBundleImpl.js:16:1010)
    at requireOrImport (C:\pw-lhc-tests\node_modules\@playwright\test\lib\transform.js:192:12)
    at TestLoader.loadTestFile (C:\pw-lhc-tests\node_modules\@playwright\test\lib\testLoader.js:53:44)
    at loadTestFilesInProcess (C:\pw-lhc-tests\node_modules\@playwright\test\lib\testLoader.js:87:40)
    at Runner._loadTests (C:\pw-lhc-tests\node_modules\@playwright\test\lib\runner.js:294:51)
    at Runner._createFilteredRootSuite (C:\pw-lhc-tests\node_modules\@playwright\test\lib\runner.js:239:39)
    at Runner._collectTestGroups (C:\pw-lhc-tests\node_modules\@playwright\test\lib\runner.js:226:34)
    at async Runner._run (C:\pw-lhc-tests\node_modules\@playwright\test\lib\runner.js:345:9)
    at async TimeoutRunner.run (C:\pw-lhc-tests\node_modules\playwright-core\lib\utils\timeoutRunner.js:46:14)
    at async raceAgainstTimeout (C:\pw-lhc-tests\node_modules\playwright-core\lib\utils\timeoutRunner.js:90:15)
    at async Runner.runAllTests (C:\pw-lhc-tests\node_modules\@playwright\test\lib\runner.js:143:20)
    at async runTests (C:\pw-lhc-tests\node_modules\@playwright\test\lib\cli.js:165:18)
    at async ji.<anonymous> (C:\pw-lhc-tests\node_modules\@playwright\test\lib\cli.js:71:7)

解决方法

方案1:降级Lighthouse到兼容CommonJS的版本

Lighthouse 10+转为纯ESM模块,而playwright-lighthouse当前仍为CommonJS,降级到v9.x可直接解决兼容问题:

  • 卸载当前Lighthouse:npm uninstall lighthouse
  • 安装v9.x稳定版:npm install lighthouse@^9.6.8 --save-dev
  • 重新运行测试:npx playwright test

方案2:改用动态导入playwright-lighthouse

在CommonJS环境中,通过动态导入方式加载ESM模块,无需修改项目整体模块类型:
修改测试文件example.spec.ts:

import { test } from '@playwright/test';

test('has title', async ({ playwright }) => {
  // 动态导入playAudit,避免CommonJS直接require ESM模块
  const { playAudit } = await import('playwright-lighthouse');
  
  const browser = await playwright['chromium'].launch({
    args: ['--remote-debugging-port=9222'],
  });
  
  const page = await browser.newPage();
  await page.goto('https://angular.io/');

  await playAudit({
    page: page,
    port: 9222,
  });

  await browser.close();
});

执行测试:npx playwright test

方案3:将项目转为ESM模式

将整个项目切换为ESM模块,适配Lighthouse 10+:

  1. 在package.json中添加"type": "module"
  2. 确保playwright.config.ts等配置文件使用ESM语法(如import/export替代require/module.exports)
  3. 运行测试:npx playwright test

内容的提问来源于stack exchange,提问作者pbaranski

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.31 04:03:19