I am trying to run Lighthouse inside a Cloud Function.
I have copied the simplest example from the README.md and added '--headless'
option.
However, I am getting this error:
Error: function terminated. Recommended action: inspect logs for termination reason. Details:
The environment variable CHROME_PATH must be set to executable of a build of Chromium version 54.0 or later.
How is it possible to do such a thing in Cloud Function?
This is the code I am uploading to the Cloud Function:
const lighthouse = require('lighthouse');
const chromeLauncher = require('chrome-launcher');
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 => {
return chrome.kill().then(() => results.lhr)
});
});
}
const opts = {
chromeFlags: ['--show-paint-rects','--headless']
};
exports.hello = async (req,res) => {
const results = await launchChromeAndRunLighthouse('https://www.google.com',opts);
// res.send(results);
}
I suspect it will not be possible to use Lighthouse on its own since it requires that env var to be set to find an existing Chrome executable. I suggest looking into a combination of puppeteer for windowless (headless) Chrome, and using Lighthouse with that.
Puppeteer is known to work on Cloud Functions, and you should be able to find out about that using a web search.
As for combining the two, it looks like there is a library that combines them both already, so I would give that a try: https://www.npmjs.com/package/google-lighthouse-puppeteer