typescript - Playwright Lighthouse-integratie met TS: Fout [ERR_REQUIRE_ESM]: require()
Ik probeer Lighthouse-tests te integreren in Playwright met het playwright-lighthouse-pakket.
Helaas krijg ik deze fout:
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.
Een project hebben zoals: 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"
}
}
En de 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();
});
Fout opgetreden na uitvoeren van: npx playwright test
Volledige 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 aangemaakt met standaardinstellingen na het uitvoeren van: npm init playwright@latest, dus het is in TypeScript
Een paar ideeën van SO geprobeerd met betrekking tot Error [ERR_REQUIRE_ESM]: require(), maar niets werkt.
Antwoord #1
Het lijkt erop dat "lighthouse": "10.0.1" niet compatibel is met het playwright-lighthouse-pakket.
Om dit probleem op te lossen, houd je aan: [email
Gewoon :
npm install [email
En de tests zouden moeten slagen.
Bekijk het probleem en de gekoppelde functie voor een update over de integratie met de nieuwste Lighthouse: https://github.com/abhinaba-ghosh/playwright-lighthouse/issues/45
Antwoord #2
Om dit probleem met [email op te lossen, probeer de versie van playwright-lighthouse bij te werken (installeer [email compatibel met Lighthouse v10):
npm install [email
en voeg toe in je package.json :
"type": "module",
Wordt gebruikt om aan te geven dat het pakket ECMAScript-modules gebruikt in plaats van CommonJS-modules. Je kunt import in je JavaScript-code gebruiken in plaats van require().
Tests moeten slagen.
