cam*_*boy 4 node.js npm electron
我正在测试一个电子应用程序,但出现了这个有害的错误:
“ TypeError:无法读取未定义的属性'on'”
我所做的研究指向错误的模块安装,语法问题或将未定义的变量传递给app.on,我怀疑问题可能是电子错误地指向了(现在它指向的是文件夹)结束于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 */
});
});
Run Code Online (Sandbox Code Playgroud)
小智 7
您可能正在尝试像以下节点应用程序一样运行您的应用程序:
$ node index.js
Run Code Online (Sandbox Code Playgroud)
电子文件是一个二进制文件,而不是JavaScript文件,当您要求它与节点一起运行时,将没有对象可以调用electron.app,因此它解析为null并且不能具有属性。就像在Electron.JS的入门文档中一样,您必须像这样运行应用程序:
更改您的package.json脚本会话以添加开始:
{
"scripts": {
"start": "electron ."
}
}
Run Code Online (Sandbox Code Playgroud)
现在运行:
$ npm start
Run Code Online (Sandbox Code Playgroud)
您发布的代码有错误,在处理和粘贴时,女巫可能是版本错误,但它应该放一些括号和大括号:
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*/
}))
}
Run Code Online (Sandbox Code Playgroud)
该应用程序现在应该正确运行。我测试了您的确切代码,删除了我没有的库,并且没有错误加载。