Skip to content

Only hide the window when closing it [Electron]

I try to hide my main window so that I hasn’t to load again later. I got the following code:

function createWindow () {
  // Create the browser window.
  win = new BrowserWindow({width: 800, height: 600})

  // Emitted when the window is closed.
  win.on('closed', (event) => {
    //win = null

So that’s not working for me, when I close the window I get this error message: enter image description here

Can somebody help me? Line 37 is the line with win.hide()

Thank you!



Use the close event instead of the closed event.

When the closed event is fired the window is already closed.

When the close event is fired the window is still open and you can prevent it from closing by using event.preventDefault(); like this:

win.on('close', function (evt) {

However on MacOS that’ll stop you from quitting your app. To allow quitting your app and preventing windows from closing use this code:

// Set a variable when the app is quitting.
var isAppQuitting = false;
app.on('before-quit', function (evt) {
    isAppQuitting = true;

win.on('close', function (evt) {
    if (!isAppQuitting) {

That’ll only stop the window from closing if the app isn’t quitting.

8 People found this is helpful