【问题标题】:Electron - "Cannot read property 'on' of undefined"; tried reinstalling,电子 - “无法读取未定义的属性‘on’”;尝试重新安装,
【发布时间】:2018-10-14 10:05:18
【问题描述】:

我正在测试一个电子应用程序,但我遇到了这个有害的错误:

“TypeError:无法读取未定义的属性'on'”

我所做的研究指出了一个拙劣的模块安装、一个语法问题,或者将一个未定义的变量传递给了 app.on,我怀疑这个问题可能是 Electron 被错误地指向(现在它被指向了)到以 electron\dist\electron.exe 结尾的文件夹,我听说可能不是正确的位置),但我不确定。

尽管检查了 require 命令、语法、重新检查、卸载和重新安装节点,但我似乎无法让这个该死的错误消失。这可能是什么原因造成的?

const electron = require('electron');
const os = require('os');
const fs = require('fs');
const app = electron.app;
const BrowserWindow = electron.BrowserWindow;
var Mousetrap = require('mousetrap');

const path = require('path');
const url = require('url');
const ipc = electron.ipcMain;

let mainWindow;


function createWindow () {
  // Create the browser window.
  mainWindow = new BrowserWindow({width: 800, height: 600})

  // and load the index.html of the app.
  mainWindow.loadURL(url.format({
    pathname: path.join(__dirname, 'index.html'),
    protocol: 'file:',
    slashes: true

/* More code in this and sub functions*/

  }))

  }
})

const preferencesManager = require('./preferencesManager');

/******
Send data to database given constructor created in preferencesManager
******/

// First instantiate the class because we want to turn the class into an object to be able to use.

const store = new Store({ //create a new getting and setting logic
  //We'll call our data file 'user-preferences'
  configName: 'user-preferences',
  defaults: {
    //800 x 600 is the default size of our window

    windowBounds: { width: 800, height: 600}
  }  

});





// When our app is ready, we'll create our BrowserWindow

app.on('ready',function(){

  //Set up a listener for what I've done in keycapture (in the renderer process)

      //???
  ipc.on('invokeAction', function(event, args){
    /* Do some action */
                });

});

【问题讨论】:

  • 我用 node index.js 测试了你的代码,它抛出了这个确切的错误。
  • 找到任何解决方案了吗?

标签: node.js npm electron


【解决方案1】:

基于电子文档here 也许您在全球范围内安装了电子。运行以下命令解决了我的问题:

npm uninstall electron
npm uninstall -g electron

【讨论】:

    【解决方案2】:

    您可能正在尝试像节点应用程序一样运行您的应用程序:

    $ node index.js
    

    electron 文件是二进制文件,而不是 JavaScript 文件,当你需要它与 node 一起运行时,将没有对象可以调用 electron.app,因此它解析为 null 并且不能具有属性。在 Electron.JS 的 getting started 文档中,您必须像这样运行应用程序:

    更改您的 package.json 脚本会话添加开始:

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

    现在运行:

    $ npm start
    

    您发布的代码有错误,可能是复制和粘贴时的版本错误,但它应该丢失一些括号和大括号:

    function createWindow () {
      // Create the browser window.
      mainWindow = new BrowserWindow({width: 800, height: 600})
    
      // and load the index.html of the app.
      mainWindow.loadURL(url.format({
        pathname: path.join(__dirname, 'index.html'),
        protocol: 'file:',
        slashes: true
    
    /* More code in this and sub functions*/
    
      }))
    
    }
    

    应用程序现在应该可以正常运行了。我测试了你的确切代码,删除了我没有的库,并且加载时没有错误。

    【讨论】:

    • 嗯...我实际上是用 npm start 运行它的。使用命令行进行故障排除,我看到 app 和 ipc 变量未定义,但其他必需的变量(如 os、fs 等)输出一个数组。 Electron 指向以下路径:“c:\xampp\htdocs\project_name\node_modules\electron\dist\electron.exe”在电子内都被设置为未定义 - 似乎问题是电子安装......但我尝试重新安装无济于事。
    • 我不太明白你的评论。当您使用 package.json 脚本中的“electron.”命令执行时,正在设置这个未定义的值?
    猜你喜欢
    • 1970-01-01
    • 2018-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-15
    • 2018-12-17
    • 2016-06-30
    • 2016-05-09
    相关资源
    最近更新 更多