Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Electron - "Cannot read property 'on' of undefined"; tried reinstalling,

I'm testing an electron app but I'm getting this pernicious error:

"TypeError: Cannot read property 'on' of undefined"

The research I've done pointed to either a botched module install, a syntax issue, or passing in an undefined variable to the app.on, and I suspect the issue may be Electron being pointed to incorrectly (now it's being pointed to the folder ending in electron\dist\electron.exe, which I've heard might not the right location), but I'm unsure.

Despite checking the require command, syntax, rechecking, uninstalling, and reinstalling node, I can't seem to make this darn error go away. What could be causing this?

const electron = require('electron');
const os = require('os');
const fs = require('fs');
const app = electron.app;
const BrowserWindow = electron.BrowserWindow;
var Mousetrap = require('mousetrap');

const path = require('path');
const url = require('url');
const ipc = electron.ipcMain;

let mainWindow;


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

  // and load the index.html of the app.
  mainWindow.loadURL(url.format({
    pathname: path.join(__dirname, 'index.html'),
    protocol: 'file:',
    slashes: true

/* More code in this and sub functions*/

  }))

  }
})

const preferencesManager = require('./preferencesManager');

/******
Send data to database given constructor created in preferencesManager
******/

// First instantiate the class because we want to turn the class into an object to be able to use.

const store = new Store({ //create a new getting and setting logic
  //We'll call our data file 'user-preferences'
  configName: 'user-preferences',
  defaults: {
    //800 x 600 is the default size of our window

    windowBounds: { width: 800, height: 600}
  }  

});





// When our app is ready, we'll create our BrowserWindow

app.on('ready',function(){

  //Set up a listener for what I've done in keycapture (in the renderer process)

      //???
  ipc.on('invokeAction', function(event, args){
    /* Do some action */
                });

});
like image 342
camelCaseCowboy Avatar asked Mar 06 '23 07:03

camelCaseCowboy


1 Answers

You are probably trying to run your application like a node app with:

$ node index.js

The electron file is a binary file, not a JavaScript file, when you require it an run with node there will be no object to call electron.app, so it parses for null and cannot have an property. As in the getting started documento of Electron.JS you must run the application like this:

Change your package.json script session adding start:

{
    "scripts": {
        "start": "electron ."
    }
}

Now run:

$ npm start

The code you posted has an error, witch may be an edition error while coping and pasting but it should loose some parenthesis and curly brackets:

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

  // and load the index.html of the app.
  mainWindow.loadURL(url.format({
    pathname: path.join(__dirname, 'index.html'),
    protocol: 'file:',
    slashes: true

/* More code in this and sub functions*/

  }))

}

The application should now run correctly. I tested you exact code, removing the libs I did not have and it loaded with no errors.

like image 86
desoares Avatar answered Mar 09 '23 00:03

desoares