2

I am attempting to get a lighthouse script running in Node.JS (which I am new to). I followed the intial instructions here https://github.com/GoogleChrome/lighthouse/blob/master/docs/readme.md#using-programmatically. I was able to complete the prior steps in the package manager console (Visual Studio 2017):

npm install -g lighthouse
lighthouse https://airhorner.com/
//and
lighthouse https://airhorner.com/ --output=json --output-path=./report/test1.json

However, I do get an initial warning that NPM only supports Node.JS in versions 4 through 8 and recommends a newer version. The problem is I am running Node v12 and NPM v5 - both the latest.

When I create a script version like below (app.js)

const lighthouse = require('lighthouse');
const chromeLauncher = require('chrome-launcher');
const config = {
    extends: 'lighthouse:default',
    settings: {
        emulatedFormFactor: 'desktop',
        onlyCategories: 'performance',
        output: 'json',
        outputPath: './report.json'
    }
};

function launchChromeAndRunLighthouse(url, opts = null, config) {
    return chromeLauncher.launch().then(chrome => {
        opts.port = chrome.port;
        return lighthouse(url, opts, config).then(results => {
            return chrome.kill().then(() => results.lhr);
        });
    });
}

// Usage:
launchChromeAndRunLighthouse('https://airhorner.com/', config).then(results => {
    // Use results!
});

And run the command

C:\src\project> node app.js

I get the error - Cannot find module 'lighthouse'

Steve Olson
  • 183
  • 2
  • 12

1 Answers1

1

don't install lighthouse locally use it inside your working dir . first start by running npm init that will create the package.json file inside the current working dir
then npm install --save lighthouse will download it and save it to node_modules now you can use it locally inside your working dir

it should look something like this

  • app.js
  • package.json
  • node_modules/

then run node app.js

Naor Tedgi
  • 5,204
  • 3
  • 21
  • 48
  • Thanks! I ran the first command, npm init, in the PM window and this is the first part of what I get back: npm WARN npm npm does not support Node.js v12.13.0 + CategoryInfo : NotSpecified: (npm WARN npm np...ode.js v12.13.0:String) [], RemoteException + FullyQualifiedErrorId : NativeCommandError npm WARN npm You should probably upgrade to a newer version of node as we can't make any promises that npm will work with this version. Supported releases of Node.js are the latest release of 4, 6, 7, 8. You can find the latest version at https://nodejs.org/ – Steve Olson Oct 29 '19 at 12:08
  • This is the second thing that comes back and it just sits here like it's doing something but nothing happens This utility will walk you through creating a package.json file. It only covers the most common items, and tries to guess sensible defaults. See `npm help json` for definitive documentation on these fields and exactly what they do. Use `npm install ` afterwards to install a package and save it as a dependency in the package.json file. Press ^C at any time to quit. – Steve Olson Oct 29 '19 at 12:11
  • OK tried again from a stand alone command prompt and was able to walk through the initialize. Install still fails though. C:\src\Lighthouse\PerformanceReport>npm install --save lighthouse npm WARN npm npm does not support Node.js v12.13.0 npm WARN npm Supported releases of Node.js are the latest release of 4, 6, 7, 8. npm[17368]: c:\ws\src\node_zlib.cc:568: Assertion `args.Length() == 7 && "init(windowBits, level, memLevel, strategy, writeResult, writeCallback," " dictionary)"' failed. – Steve Olson Oct 29 '19 at 12:29
  • I've tried this multiple times with multiple versions of node rebooting in between installs. They all give the same compatibility warnings. In looking at the versions of npm that installs with node, none of them seem correct. For example, NodeJS.org says npm v6 comes with node v12 - it does not. You get version 5.3 - which is an older version then what comes with node v10?!?! Can I do this from a windows machine? – Steve Olson Oct 29 '19 at 12:58
  • This is now working. Apparently there is a bug in NODEJS requiring a seperate uninstall of NPM to get the versions in sync. https://stackoverflow.com/questions/47226238/npm-warn-npm-npm-does-not-support-node-js-v9-1-0 – Steve Olson Oct 29 '19 at 16:55