无法读取电子 javascript 中未定义的属性“on”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46977296/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cannot read property 'on' of undefined in electron javascript
提问by Sharjeel shahid
I am trying to run this code but every time I am getting this error message. First I installed npmglobally. Then I installed it within my app but still getting same error.
我正在尝试运行此代码,但每次收到此错误消息时。首先我npm全局安装。然后我将它安装在我的应用程序中,但仍然出现相同的错误。
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
未捕获的类型错误:无法读取对象中未定义的属性“on”。(H:\electric\main.js:12:4) 在 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) 在 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]
回答by MooMoo
I guess you are trying to run electron using node. Does your package.json look like this?
我猜您正在尝试使用 node.js 运行电子。你的 package.json 看起来像这样吗?
{
"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
当您想在代码require(electron) 中使用电子时, 您应该使用此命令将全局路径链接到当前目录
npm link electron
回答by user8675309
I had a similar issue. On my machine I have to use
我有一个类似的问题。在我的机器上我必须使用
npm start
npm start
instead of the electron command or it gives me trouble.
而不是电子命令,否则会给我带来麻烦。
Thanks kontrollanten
感谢控制
回答by Beezer
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:
我需要查看您的 html 才能确定,但我遇到了这个问题,这是由在我的 html 中链接 js 引起的。基本上,您要启动应用程序两次。一次,当你启动时 node 运行“electronwhatever.js”,再次因为它链接在你的 html 中。例如:
my app is being created in index.js, index.html is what my app is loading.
我的应用程序是在 index.js 中创建的,index.html 是我的应用程序正在加载的内容。
index.html has "
index.html 有“
script src="index.js"
脚本 src="index.js"
", so it's firing off twice.
",所以它发射了两次。
I commented out the script and i no longer got the error.
我注释掉了脚本,不再出现错误。
回答by Trail3lazer
I have actually run into the same issue. I learned that you can only require(electon) once, or else it will only be defined for the last variable that called it. Instead of doing this:
我实际上遇到了同样的问题。我了解到你只能 require(electon) 一次,否则它只会为调用它的最后一个变量定义。而不是这样做:
const electron = require('electron');
const {Menu} = require('electron');
const {app} = require('electron');
const {BrowserWindow} = require('electron');
Do this:
做这个:
const { Menu, app, BrowserWindow } = require('electron');
回答by DanteTheSmith
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 installis used electron will be locally installed in your node_modules folder.
如果使用标志,则需求将存储在 package.json 中,npm install使用时电子将本地安装在您的 node_modules 文件夹中。
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
您还可以查看电子的 npm 文档(每个 npm 模块都可以这样做 - 结果我的质量各不相同): npm -electron
回答by Darshan Jain
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.
我检查了我的 html 文件。问题是我已经包含了我的 javascript 文件两次。一次在 package.json 文件中,第二次在 html 中。所以,我已经删除了 html 中包含的 javascript。这样,问题就解决了。
回答by Darshan Jain
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')

