Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot read property 'on' of undefined in electron javascript

I am trying to run this code but every time I am getting this error message. First I installed npm globally. Then I installed it within my app but still getting same error.

Uncaught TypeError: Cannot read property 'on' of undefined at Object. (H:\electric\main.js:12:4) at Object. (H:\electric\main.js:63:3) at Module._compile (module.js:571:32) at Object.Module._extensions..js (module.js:580:10) at Module.load (module.js:488:32) at tryModuleLoad (module.js:447:12) at Function.Module._load (module.js:439:3) at Module.require (module.js:498:17) at require (internal/module.js:20:19) at file:///H:/electric/views/login.html:2:3

const electron = require('electron');
const {Menu} = require('electron');
const {app} = require('electron');
const {BrowserWindow} = require('electron');
const conn = require('mysql');
const path = require('path');
const url = require('url');

// const app = electron.app;
// const BrowserWindow = electron.BrowserWindow;
var mainWindow = null;
app.on('ready', function () {
    mainWindow = new BrowserWindow({ width: 1024, height: 768, backgroundcolor: '#2e2c29' });
    mainWindow.loadURL(url.format({
        pathname: 'popupcheck.html',
        protocol: 'file:',
        slashes: true
    }));enter code here
    mainWindow.webContents.openDevTools();
    mainWindow.setProgressBar(1);
});`][1]
like image 836
Sharjeel shahid Avatar asked Oct 27 '17 14:10

Sharjeel shahid


1 Answers

I guess you are trying to run electron using node. Does your package.json look like this?

{
    "scripts": {
        "start": "node main.js"
    }
}

please change to run electron app like this

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

It should work

note: this is extra for somebody that install electron to global with command something like this

npm install -g electron

when you want to use electron in the code require(electron) you should link the global path to your current directory by using this command

npm link electron
like image 167
MooMoo Avatar answered Oct 18 '22 00:10

MooMoo