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.

Running lighthouse on Nodejs server

See original GitHub issue

I’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:closed
  • Created 4 years ago
  • Comments:7

github_iconTop GitHub Comments

7reactions
marvinjudecommented, Aug 16, 2019

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

1reaction
patrickhulcecommented, Aug 15, 2019

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

Read more comments on GitHub >

github_iconTop 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 >

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