Cannot create browserwindow before app is ready


#1

Hi, guys, on os x, I sometimes get this error “cannot create browserwindow before app is ready”. Below are the code in my main.js:

var mainWindow = null;

var init = function() {
  var options = {
    width: 1200,
    height: 800,
  };

  mainWindow = new BrowserWindow(options);
  mainWindow.focus();


  webContents.loadURL('file://' + __dirname + '/browser/index.html');

  mainWindow.on('closed', function() {
    mainWindow = null;
  });
};

app.on('ready', function() {
  init();
});

app.on('activate', function(e, hasVisibleWindows) {
  if (!hasVisibleWindows) {
    init();
  }
});

Any ideas?


#2

activate fires before ready. active fires, hasVisibleWindows = false, so !hasVisibleWindows = true, enter the conditional, call init(); try to make a BrowserWindow, app has not yet fired ‘ready’