Hi,
Having issues generating a report which used to work in v2.
Example:
const fs = require('fs');
const lighthouse = require('lighthouse');
const chromeLauncher = require('chrome-launcher');
const ReportGenerator = require('lighthouse/lighthouse-core/report/report-generator');
function launchChromeAndRunLighthouse(url, opts, config = null) {
return chromeLauncher.launch({chromeFlags: opts.chromeFlags}).then(chrome => {
opts.port = chrome.port;
return lighthouse(url, opts, config).then(results => {
// use results.lhr for the JS-consumeable output
// https://github.com/GoogleChrome/lighthouse/blob/master/typings/lhr.d.ts
// use results.report for the HTML/JSON/CSV output as a string
// use results.artifacts for the trace/screenshots/other specific case you need (rarer)
delete results.artifacts;
return chrome.kill().then(() => results.lhr)
});
});
}
const opts = {
chromeFlags: ['--headless', '--no-sandbox']
};
// Usage:
launchChromeAndRunLighthouse('https://www.google.com/', opts).then(results => {
let report = new ReportGenerator().generateReport(results, 'html');
fs.writeFileSync("report.html", report);
});
Error:
(node:19128) UnhandledPromiseRejectionWarning: TypeError: (intermediate value).generateReport is not a function
at launchChromeAndRunLighthouse.then.results (..\index.js:26:37)
at <anonymous>
(node:19128) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2)
(node:19128) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
Any suggestions?
Lighthouse version 3.0.3
TypeError: (intermediate value).generateReport is not a function
Instead of:
let report = new ReportGenerator().generateReport(results, 'html');
fs.writeFileSync("report.html", report);
Should be:
const report = ReportGenerator.generateReport(results, 'html');
fs.writeFileSync("report.html", report);
Also protip: in v3 of Lighthouse you can ask for the HTML directly from Lighthouse.
const {report} = await lighthouse('https://example.com', {output: ['html', 'csv', 'json']}, config)
fs.writeFileSync('report.html', report[0])
fs.writeFileSync('report.csv', report[1])
fs.writeFileSync('report.json', report[2])
Thank you! Also for the "protip". Would be nice to see the fully working example in https://github.com/GoogleChrome/lighthouse/blob/master/docs/readme.md#using-programmatically as it wasn't very descriptive on how to use the results :)
Most helpful comment
Also protip: in v3 of Lighthouse you can ask for the HTML directly from Lighthouse.