Search code examples
node.jselectronready

NodeJS Electron app.on('ready', ...) question


I have an app that on ready calls this function from a module.

// APP READY
app.on('ready', createWindow.main);

That works without any problems.

However when I try

app.on('ready', () => {
  console.log('Ready');
  createWindow.main;
});

the console shows "Ready" but nothing happens after that.

The idea is that I need, once the app is ready, to call different functions.

What could be the problem here?

I'm calling the windows.js file like this:

const createWindow = require('./windows');

windows.js

const {session} = require('electron');
const app = require('electron').app;
const BrowserWindow = require('electron').BrowserWindow;
const url = require('url');
const path = require('path');
const settings = require('electron-settings');

// Windows Variables
let mainWindow;

// Functions container
var createWindow = {};

// MAIN WINDOW
createWindow.main = function() {
    console.log('Creating main window');
    mainWindow = new BrowserWindow({
      width: 800,
      height: 600
    });

    mainWindow.loadURL(url.format({
      protocole: 'file:',
      slashes: true,
      pathname: path.join(__dirname, 'html/index.html')
    }));

    mainWindow.on('closed', () => {
      mainWindow = null;
      app.quit();
    });

};

module.exports = createWindow;

Solution

  • You have to call main in the second case

    app.on('ready', () => {
      console.log('Ready');
      createWindow.main(); // <-- !
    });