Closed AxelTerizaki closed 4 years ago
describe repro
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.
Thanks for the code. So minimal steps are:
foo.js
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');
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.
I fixed this in 1.13.4. Please update to the latest
Thanks for the quick fix! That works just fine :)
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
andremote
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 likeremote?.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.