Cannot read property 'on' of undefined in electron javascript

谁说胖子不能爱 提交于 2019-12-10 13:15:21

问题


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]

回答1:


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



回答2:


I had a similar issue. On my machine I have to use

npm start

instead of the electron command or it gives me trouble.

Thanks kontrollanten




回答3:


I'd need to see your html to be sure, but i had this problem and it was caused by linking the js in my html. Basically, you're starting the app twice. Once, when node runs 'electron whatever.js' when you start, and again because it's linked in your html. eg:

my app is being created in index.js, index.html is what my app is loading.

index.html has "

script src="index.js"

", so it's firing off twice.

I commented out the script and i no longer got the error.




回答4:


Clearly, app is not defined. Which means:

const {app} = require('electron');

Can not find the requirement you asked for. Are you sure you installed electron to appropriate folder / at all?

Run in project or module root:

npm list --depth=0

to get a list of your node packages.

Is electron there? If not:

npm install electron (with or without an optional flag --save)

from project root folder.

If flag is used requirement will be stored in package.json and when npm install is used electron will be locally installed in your node_modules folder.

You can also take a look at electron's npm documentation (same can be done with every npm module - results my vary in quality tho): npm - electron




回答5:


I checked my html file. The problem was I have included my javascript files twice. Once in the package.json file and the second time in html. So, I have removed the javascript included from html. Thus, the issue is resolved.




回答6:


Try to remove this old package and install this one if you didn't installed yet:

git clone https://github.com/electron/electron-quick-start
cd electron-quick-start
npm install
npm start

if you got the same error then do the fix yourself :

const electron = require('electron');
const app = require('app');
const BrowserWindow = require('browser-window')


来源:https://stackoverflow.com/questions/46977296/cannot-read-property-on-of-undefined-in-electron-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!