Playwright Lighthouse integration with TS: Error [ERR_REQUIRE_ESM]: require()

2.7k Views Asked by At

I'm trying to integrate Lighthouse tests in Playwright with playwright-lighthouse package.
Unfortunately I get this error:

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.

Having project like: 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"
  }
}

And the test 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 occure after running: npx playwright test

Full stacktrace:

 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)

OS: Windows 11, Node 18

Project created with defaults after running: npm init playwright@latest so is in TypeScript

Tried couple of ideas form SO regarding Error [ERR_REQUIRE_ESM]: require() but nothing works.

2

There are 2 best solutions below

1
On

It looks like "lighthouse": "10.0.1" is incompatible with playwright-lighthouse package.

To solve this issue stick with: [email protected]

Just:

npm install [email protected]

And tests should pass.

Watch issue and linked feature for update for integration with newest lighthouse: https://github.com/abhinaba-ghosh/playwright-lighthouse/issues/45

0
On

To solve this issue with [email protected], try to upgrade playwright-lighthouse version (install [email protected] compatible with Lighthouse v10 ):

 npm install [email protected]

and add in your package.json :

  "type": "module",

Used to specify that the package is using ECMAScript modules instead of CommonJS modules. You can use import in your JavaScript code instead of require().

Tests should pass.