node.js - 电子 - “无法读取未定义的属性‘on’”;尝试重新安装,
问题描述
我正在测试一个电子应用程序,但我收到了这个有害的错误:
“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 文件,当您需要它与 node 一起运行时,将没有对象可以调用 electron.app,因此它解析为 null 并且不能具有属性。在 Electron.JS 的入门文档中,您必须像这样运行应用程序:
更改您的 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*/
}))
}
该应用程序现在应该可以正常运行。我测试了您的确切代码,删除了我没有的库,并且加载时没有错误。
推荐阅读
- html - Italic tag not being formatted in Github Pages
- python - 使用 Python 调用 JSON API
- firebase - 如何将 Google App Engine 与 Firebase 结合使用
- php - Symfony 翻译忽略参数中的花括号
- wordpress - 如何在 wordpress 主题中包含 json 清单文件
- python - “while”循环未按预期运行
- java - Spring批处理GZIP ItemWriter/ItemReader
- python - 如何使用 egg 安装 scikit-image?
- python - 字符从摩尔斯电码错误地翻译成英文
- ios - SDWebImage 不适用于 Firebase 存储