4

A strange issue I am facing while developing an electron app. What I am trying to achieve is use lighthouse to conduct page audits. I am using it programmatically like this

const command = `lighthouse ${website} --quiet --chrome-flags=--headless --output-path=${outputPath}   --output html --emulated-form-factor=${strategy}  --only-categories=${options}`;

os.execCommand(command, function() {
      res.send(response);
});

What it does is executes a lighthouse as a command in Main thread(nodejs). I have mentioned lighthouse as a dependency in package.json and it works perfectly in development mode.

The strange part is, when the package is created it gives me an error lighthouse is not a recognized command

How can I resolve this dependency ? or provide the command path from node_modules>

Kindly guide. PS: Unable to use lighthouse as a module using require. Also This is a standalone application hosting this service internally.

Parag Diwan
  • 3,007
  • 2
  • 19
  • 37

2 Answers2

3

You could install lighthouse as a global dependency. If you don't want to do that, use npm bin to get the folder where npm will install executables. Then, use absolute path of lighthouse. https://stackoverflow.com/a/15157360/10674906 explains how to do it better.

Rinshad
  • 116
  • 4
  • hacky solution I tried is including `node_modues` in the package using `extraFiles` attribute. Not a good solution at all. Hope for better solution. – Parag Diwan Aug 15 '20 at 15:14
  • I could make it work. I installed lighthouse globally and I am using `exec` from `child_process` module to run the command. Also, I am running this in the main process of Electron. – Rinshad Aug 15 '20 at 18:43
  • This would work for youself. Think from application perspective. – Parag Diwan Aug 16 '20 at 01:35
0

Could you start a child process and run lighthouse through npx Allowing npx to resolve dependency space rather than relying on os

const {app, BrowserWindow} = require('electron')
const childproc = require('child_process')
const path = require('path')

function createWindow () {
  // Create the browser window.
  const mainWindow = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
      preload: path.join(__dirname, 'preload.js')
    }
  })

  // and load the index.html of the app.
  mainWindow.loadFile('index.html')

  // Open the DevTools.
  mainWindow.webContents.openDevTools()
}


app.whenReady().then(() => {
  createWindow()
  
  app.on('activate', function () {
    if (BrowserWindow.getAllWindows().length === 0) createWindow()
  })
})

app.on('window-all-closed', function () {
  if (process.platform !== 'darwin') app.quit()
})

const child = childproc.spawn(
  'npx',
  [
    'lighthouse',
    `${website}`,
    `--quiet`,
    `--chrome-flags=--headless`,
    `--output-path=${outputPath}`,
    `--output`,
    `html`,
    `--emulated-form-factor=${strategy}`,
    `--only-categories=${options}`
  ],
)

child.on('exit', function (code, signal) {
  console.log(
    'child process exited with ' + `code ${code} and signal ${signal}`
  )
})

child.on('error', (err) => {
  console.log(err)
})


child.stdout.on('data', (data) => {
  console.log(data.toString())
})

child.stderr.on('data', (data) => {
  console.error(`child stderr:\n${data}`)
})

Not really sure about your whole setup but built this example with the starter and ran lighthouse locally...

$ npm start

> electron-quick-start@1.0.0 start 
> electron .

child stderr:
Please provide a url

child stderr:

Specify --help for available options

child process exited with code 1 and signal null
ContextCue
  • 323
  • 5
  • 14