简体   繁体   中英

Uncaught TypeError: Cannot read property 'on' of undefined. I have tried to reinstall electron as well

I am not able to remove an error mentioned:

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

I have tried to reinstall electron as well but still getting the same error.

 const electron = require('electron');
 const {BrowserWindow, Menu} = require('electron');
 var app = electron.app;
 const conn = require('mysql');
 const path = require('path');
 const url = require('url');
 var mainWindow;
 app.on('ready', function () {
     mainWindow = new BrowserWindow({ width: 1024, height: 768, 
     backgroundcolor: 'black' });
        mainWindow.loadURL(url.format({
            pathname: 'dashboard.html',
            protocol: 'file:',
            slashes: true
       }));
     mainWindow.webContents.openDevTools();
     mainWindow.setProgressBar(1);
});

"TypeError: Cannot read property 'on' of undefined" This means 'app' object has not been loaded properly from 'electron' module and the 'app.on()' method on 8th line, it has been called on undefined object 'app'.

Please try these possible solutions.

  1. Replace

    const electron = require('electron'); const {BrowserWindow, Menu} = require('electron'); var app = electron.app;

    with

    var app = require('electron').app; const {BrowserWindow, Menu} = require('electron');
  2. In Package.json file edit/add this line specifying the starting point for an application.

     "script":{"start": "electron ."}

edit:

May be you have the npm electron module installed somewhere in your system(globally), it overrides the builtin electron module. You can find out the path of it by logging out

 require.resolve('electron').

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM