Playwright集成Lighthouse报错:Cannot find module 'lighthouse/report/generator/report-generator'
Hey there! Let's work through this issue you're hitting when trying to pull performance metrics with Playwright + Lighthouse using the playwright-lighthouse library. Since I can’t see your error screenshot, I’ll walk you through the most common pitfalls and fixes that usually resolve these kinds of problems.
Common Fixes to Try
1. Verify Version Compatibility
Version mismatches between playwright, lighthouse, and playwright-lighthouse are one of the top causes of errors.
- Check the library's documentation for recommended version pairings (you can run
npm listto see your current versions and cross-reference with the library's README notes). - Try reinstalling dependencies with tested compatible versions. For example, you can lock in specific versions in your
package.jsonor run:npm uninstall playwright-lighthouse playwright lighthouse npm install playwright-lighthouse@^3.1.0 playwright@^1.35.0 lighthouse@^11.4.0
2. Double-Check Configuration Setup
Lighthouse needs to connect to Playwright's browser instance correctly—small config mistakes break this connection:
- Ensure you’re extracting the correct port from Playwright’s WebSocket endpoint. The standard code for this is:
const port = new URL(browser.wsEndpoint()).port; - Make sure you’re not missing critical browser launch arguments, especially in restricted environments (like CI). Add these when launching your browser:
const browser = await playwright.chromium.launch({ args: ['--no-sandbox', '--disable-setuid-sandbox'], headless: true // or false, depending on your testing needs });
3. Fix Environment & Permission Issues
- CI Environments: Most CI platforms block browser sandboxes by default. The
--no-sandboxargument mentioned above is mandatory here. Also, ensure your runner has enough CPU/memory to run both Playwright and Lighthouse at the same time. - Local Environments: Close any other processes that might be using the same port as Playwright, or specify a fixed port in your browser launch config to avoid conflicts.
4. Audit Your Code for Async/Resource Management
- Always use
awaitfor all Playwright and Lighthouse calls—missing async/await leads to race conditions where Lighthouse tries to run on a closed browser instance. - Don’t close the Playwright browser or context until after Lighthouse has finished running. Your code flow should follow this order:
// Launch browser and create page const browser = await playwright.chromium.launch(...); const page = await browser.newPage(...); // Run Lighthouse audit await lighthouse(page.url(), { port: port }, lighthouseConfig); // Close browser ONLY after Lighthouse completes await browser.close();
Next Steps
If you can share the exact error text from your screenshot (even just a snippet), I can give you a more targeted solution. But these steps should resolve most common issues with this integration.
内容的提问来源于stack exchange,提问作者Sujith

