webusb icon indicating copy to clipboard operation
webusb copied to clipboard

Electron crash on start

Open gavinbenda opened this issue 4 years ago • 5 comments

Can't find much on this around the net, but including webusb within an electron app (just using the blank/quick-start guide to keep it simple), and installing webusb, then running the webusb example code from the docs, causes an immediate crash on attempting to start the app.

Unfortunately there doesn't seem to be much in the way of errors.

Node.js 12.13.0, Chromium 80.0.3987.141, and Electron 8.1.1 on OSX

gavinbenda avatar Mar 23 '20 07:03 gavinbenda

I've not tried this in electron, do you see an error when starting electron from the command line?

thegecko avatar Mar 23 '20 08:03 thegecko

Unfortunately all of the debugging disconnects... It's the same issue as this comment I believe which is the only other mention of it on the web! https://github.com/electron/electron/issues/14545#issuecomment-569835883

gavinbenda avatar Mar 23 '20 08:03 gavinbenda

I assume you are using it in the backend process? The underlying usb package wont be available in the browser process.

thegecko avatar May 17 '20 08:05 thegecko

you can require('usb') from the renderer, you just need init in the non-renderer process to look a little bit like this

const electron = require('electron')

const createWindow = () => {
  const mainWindow = new electron.BrowserWindow({
    width: 1024,
    height: 768,
    title: 'OBD Flasher',
    webPreferences: {
      nodeIntegration: true,
      backgroundThrottling: false
    }
  })
  mainWindow.loadURL(`file://${__dirname}/index.html`)
}

const run = async () => {
  // power saver
  electron.powerSaveBlocker.start('prevent-display-sleep')
  // native modules
  electron.app.allowRendererProcessReuse = false
  // no backgrounding
  electron.app.commandLine.appendSwitch('disable-renderer-backgrounding')
  // init
  await electron.app.whenReady()
  createWindow()
  electron.app.on('activate', () => {
    if (electron.BrowserWindow.getAllWindows().length === 0) {
      createWindow()
    }
  })
  electron.app.on('window-all-closed', () => {
    if (process.platform !== 'darwin') {
      electron.app.quit()
    }
  })
}

run()

brandonros avatar Jul 23 '20 15:07 brandonros

then from my renderer.js i call require('usb') and it works fine.

thegecko and i just whipped up a new release that fixes a segfault on windows during device opening, i bet you it will fix this electron issue too

see https://github.com/thegecko/webusb/issues/68

brandonros avatar Jul 23 '20 15:07 brandonros