Running lighthouse on Nodejs server
See original GitHub issueI’m trying to run lighthouse audit on a Nodejs server, I’ve tried different code samples in the docs but it’s not working for me.
Basically, I’m trying to create an API that returns the lighthouse result for a website
const chromeLauncher = require('chrome-launcher');
const puppeteer = require('puppeteer');
const lighthouse = require('lighthouse');
const request = require('request');
const express = require('express');
const util = require('util');
const app = express();
const asyncMiddleware = fn => (req, res, next) => {
Promise.resolve(fn(req, res, next)).catch(next);
};
async function launchChromeAndRunLighthouse() {
const URL = 'https://www.chromestatus.com/features';
const opts = {
chromeFlags: ['--headless'],
logLevel: 'info',
output: 'json'
};
// Launch chrome using chrome-launcher.
const chrome = await chromeLauncher.launch(opts);
opts.port = chrome.port;
// Connect to it using puppeteer.connect().
const resp = await util.promisify(request)(
`http://localhost:${opts.port}/json/version`
);
const { webSocketDebuggerUrl } = JSON.parse(resp.body);
const browser = await puppeteer.connect({
browserWSEndpoint: webSocketDebuggerUrl
});
// Run Lighthouse.
return lighthouse(URL, opts, null).then(async result => {
await browser.disconnect();
await chrome.kill();
return result.lhr;
});
}
app.get(
'/',
asyncMiddleware(async (req, res, next) => {
const results = await launchChromeAndRunLighthouse('https://google.com');
res.json(results);
})
);
app.listen(process.env.PORT || 8080, () => console.log('Running'));
Issue Analytics
- State:
- Created 4 years ago
- Comments:7
Top Results From Across the Web
How to Use and Customize Lighthouse - CLI & NodeJS
An introductory guide to the different parts of Google Lighthouse, how to use it via CLI and NodeJS, and strategies for customizing its ......
Read more >Generating Lighthouse Scores using Node.js - GeeksforGeeks
Generating Lighthouse Scores using Node.js · Run two loops: one for mobile and other for desktop scores by specifying options. · Call the ......
Read more >lighthouse - npm
Start using lighthouse in your project by running `npm i lighthouse`. There are 315 other projects in the npm registry using lighthouse.
Read more >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 >Build a Node.js Tool to Record and Compare Google ...
In this tutorial, I'll show you step by step how to create a simple tool in Node.js to run Google Lighthouse audits via...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Thanks @patrickhulce
For anyone experiencing this issue with heroku, I fixed it by adding a Google Chrome buildpack to my deployment 😉
https://elements.heroku.com/buildpacks/heroku/heroku-buildpack-google-chrome#buildpack-instructions
This is venturing into general debugging advice @marvinjude which isn’t what the Github issues are intended for. If you find a specific bug with Lighthouse, please feel free to come back with a fresh issue.
PS - the logs say
ChromePathNotSetError
so I’m guessing you haven’t set the Chrome path in heroku