question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Is there a way to generate html report using lighthouse programmatically.

See original GitHub issue

Sorry if I missed this anywhere but I am trying to find how to generate the html using lighthouse using the example given in docs. https://github.com/GoogleChrome/lighthouse/blob/HEAD/docs/readme.md#using-programmatically

I could generate the html by calling lighthouse-cli directly but could not do it using above script. My opts are:

const opts = {
  chromeFlags: ['--show-paint-rects'],
  output: 'html',
  'output-path': './lighthouse-results.html',
  'save-assets': true
};

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:16 (2 by maintainers)

github_iconTop GitHub Comments

18reactions
jbustamovejcommented, Oct 9, 2018

I did it like this:

const lighthouse = require('lighthouse');
const ReportGenerator = require('lighthouse/lighthouse-core/report/report-generator');

const report = await lighthouse(page.url(), { port: port }, config).then(results => {
  return results;
});
const html = ReportGenerator.generateReport(report.lhr, 'html')

MIght be worth it to update the docs for this.

17reactions
taytuscommented, Jun 3, 2020

@patrickhulce thank you for that!

I’m still confused and I did it work in a hacky way. All I want is to save the HTML reports in a specific location. This is my solution but I would love feedback if there is a better way to do this.

Thank you!!

```javascript
const fs = require('fs');
const lighthouse = require('lighthouse');
const chromeLauncher = require('chrome-launcher');
const log = require('lighthouse-logger');

(async () => {
  log.setLevel('info');
  const chrome = await chromeLauncher.launch({chromeFlags: ['--headless']});
  const options = {output: 'html', onlyCategories: ['performance'], port: chrome.port};
  const runnerResult = await lighthouse('https://example.com', options);

  // `.report` is the HTML report as a string
  const reportHtml = runnerResult.report;
  fs.writeFileSync('lhreport.html', reportHtml);

  // `.lhr` is the Lighthouse Result as a JS object
  console.log('Report is done for', runnerResult.lhr.finalUrl);
  console.log('Performance score was', runnerResult.lhr.categories.performance.score * 100);

  await chrome.kill();
})();
Read more comments on GitHub >

github_iconTop Results From Across the Web

An Introduction To Running Lighthouse Programmatically
One of the simplest ways to run Lighthouse is through Chrome's DevTools Lighthouse panel. If you open your site in Chrome and then...
Read more >
Generating HTML report in gulp using lighthouse
The answer from @EMC is fine, but it requires multiple steps to generate the HTML from that point. However, you can use it...
Read more >
Running Lighthouse Programmatically - The Publishing Project
The simplest example will run all Lighthouse reports for the given URL using the default options for Lighthouse. function ...
Read more >
Actually Running Google Lighthouse Programmatically
The fastest way to run Lighthouse Batch programmatically is to use the -s method, shown below. Essentially the -s option allows you to...
Read more >
Lighthouse overview - Chrome Developers
You give Lighthouse a URL to audit, it runs a series of audits against the page, and then it generates a report on...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found