Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remote does not exist when app is launched without Electron run-time #58

Closed
AxelTerizaki opened this issue Jun 4, 2020 · 6 comments
Closed

Comments

@AxelTerizaki
Copy link

Hello.

I upgraded to the latest version of about-window (1.13.3) but it broke my app during its CI.

Since it's an electron app, we also have a mode to run it without the electron runtime, namely for debug purposes and launch in CI (since the app can also do some CLI stuff)

When not loaded via the Electron runtime, the app and remote objects are undefined. This is normally not a problem as in my own application I test if app is defined before doing anything with it.

However, on line 5 of index.ts the code tries to get remote.app except that since remote is undefined, it crashes my application on startup when importing, since the code is at the top level of the module and is executed, wether electron is loaded or not.

A quick fix would be to test if remote exists as well before trying to use remote.app or if you're using a recent typescript, an optional chaining like remote?.app which works wonders in those cases.

For now I pinned my version of about-window to 1.13.2 while waiting for a fix :)

Thanks in advance.

@rhysd
Copy link
Owner

rhysd commented Jun 4, 2020

describe repro

@AxelTerizaki
Copy link
Author

Sample code taken from Electron docs :

const { app, BrowserWindow } = require('electron')
const openAboutWindow = require('about-window')

function createWindow () {
  let win = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
      nodeIntegration: true
    }
  })

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

if (app) app.whenReady().then(createWindow);

// Do something else with the app that doesn't require electron

console.log('Yeah');

Here if (app) ensures that we won't try to load the GUI if app is undefined. If it's run with node's runtime (node index.js), app will be undefined, thus the code is avoided, and the code can proceed with stuff that doesn't require Electron.

The problem is that requiring about-window executes the top-level code of the module, which assumes electron is loaded, and thus crashes the main app with an exception. app isn't even the problem here, remote.app is, in about-window code, since remote does not exist in this case.

Hope that helps.

@rhysd
Copy link
Owner

rhysd commented Jun 5, 2020

Thanks for the code. So minimal steps are:

  1. Save below code as foo.js
  2. Run node foo.js
const openAboutWindow = require('about-window')

If it's run with node's runtime (node index.js), app will be undefined

This package does not support the usage without Electron.

Though I'm still not sure to understand the situation, if you want to write up code which supports both Electron and Node.js, you would need to write code like below:

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

function createWindow () {
  // Run only on Electron
  const openAboutWindow = require('about-window')

  let win = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
      nodeIntegration: true
    }
  })

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

if (app) app.whenReady().then(createWindow);

// Do something else with the app that doesn't require electron

console.log('Yeah');

@AxelTerizaki
Copy link
Author

I used require for the example with JS, but I'm using import (via typescript) and imports are not conditionnal, they're loaded up no matter what, hence my request.

Version 1.13.2 worked just fine, the bug was introduced in 1.13.3 with the call to remote.app without verifying if remote exists.

@rhysd rhysd closed this as completed in 6180171 Jun 5, 2020
@rhysd
Copy link
Owner

rhysd commented Jun 5, 2020

I fixed this in 1.13.4. Please update to the latest

@AxelTerizaki
Copy link
Author

Thanks for the quick fix! That works just fine :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants