Node.js 电子–“;无法读取属性';在';“未定义”的定义;;尝试重新安装,

Node.js 电子–“;无法读取属性';在';“未定义”的定义;;尝试重新安装,,node.js,npm,electron,Node.js,Npm,Electron,我正在测试一个electron应用程序,但我遇到了一个致命的错误: “TypeError:无法读取未定义的属性“on” 我所做的研究指出,要么是模块安装错误,要么是语法问题,要么是将未定义的变量传递到app.on,我怀疑问题可能是Electron被错误地指向了(现在它被指向了以Electron\dist\Electron.exe结尾的文件夹,我听说可能不是正确的位置),但我不确定 尽管检查了require命令、语法、重新检查、卸载和重新安装节点,但我似乎无法消除这个该死的错误。这可能是什么原因造

我正在测试一个electron应用程序,但我遇到了一个致命的错误:

“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
electron文件是一个二进制文件,而不是JavaScript文件,当您要求它与节点一起运行时,将没有对象可调用electron.app,因此它解析为null,并且不能有属性。在Electron.JS的documento中,您必须按如下方式运行应用程序:

更改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*/

  }))

}

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

基于electron文档 也许你在全球安装了electron。运行以下命令解决了我的问题:

npm uninstall electron
npm uninstall -g electron

我用node index.js测试了你的代码,它抛出了这个错误。找到解决方案了吗?嗯。。。我实际上是在用npm start运行它。使用命令行进行故障排除,我发现app和ipc变量未定义,但其他必需变量(如os、fs等)输出一个数组。Electron导致以下路径:“c:\xampp\htdocs\project\u name\node\u modules\Electron\dist\Electron.exe”考虑到我当前需要路径文件,但调用Electron中的任何方法,如app、BrowserWindow和ipc,都被设置为未定义-似乎问题在于Electron安装。。。但我试着重新安装,但没有用。我真的不明白你的评论。当您使用package.json脚本中的“electron.”命令执行时,会设置此未定义的值吗?