无法读取电子 javascript 中未定义的属性“on"

2022-01-10 00:00:00 electron javascript

我正在尝试运行此代码,但每次都收到此错误消息.首先,我全局安装了 npm.然后我在我的应用程序中安装了它,但仍然出现同样的错误.

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.

未捕获的类型错误:无法读取未定义的属性on"目的.(H:electricmain.js:12:4) 在对象处.(H:electricmain.js:63:3) 在 Module._compile (module.js:571:32) 在Module.load 中的 Object.Module._extensions..js (module.js:580:10)(module.js:488:32) 在 tryModuleLoad (module.js:447:12) 在Module.require 处的 Function.Module._load (module.js:439:3)(module.js:498:17) 在需要 (internal/module.js:20:19) 在file:///H:/electric/views/login.html:2:3

Uncaught TypeError: Cannot read property 'on' of undefined at Object. (H:electricmain.js:12:4) at Object. (H:electricmain.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]

推荐答案

我猜你正在尝试使用 node.js 运行 electron.你的 package.json 是这样的吗?

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 ."
    }
}

它应该可以工作

注意:对于使用类似命令将电子安装到全局的人来说这是额外的

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

npm install -g electron

当你想在代码中使用电子时require(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

相关文章