#node.js #electron
#node.js #electron
Вопрос:
Я пытаюсь создать простое приложение на базе Electron, основываясь на инструкциях наhttp://electron.atom.io/docs/tutorial/quick-start / , которые предоставляют приведенный ниже код. Когда я пытаюсь запустить его с electron 1.4.4 и NodeJS 6.7.0, я получаю:
TypeError: Cannot read property 'on' of undefined
at Object.<anonymous> (/path/to/proj/src/goelectron.js:29:4)
Код следующий:
const {app, BrowserWindow} = require('electron')
// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let win
function createWindow () {
// Create the browser window.
win = new BrowserWindow({width: 800, height: 600})
// and load the index.html of the app.
win.loadURL(`file://${__dirname}/index.html`)
// Open the DevTools.
win.webContents.openDevTools()
// Emitted when the window is closed.
win.on('closed', () => {
// Dereference the window object, usually you would store windows
// in an array if your app supports multi windows, this is the time
// when you should delete the corresponding element.
win = null
})
}
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('ready', createWindow)
// Quit when all windows are closed.
app.on('window-all-closed', () => {
// On macOS it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd Q
if (process.platform !== 'darwin') {
app.quit()
}
})
app.on('activate', () => {
// On macOS it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (win === null) {
createWindow()
}
})
// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and require them here.
Это говорит о том, что ‘app’ не определено, но я не знаком с обозначением, которое предоставляет const {app, BrowserWindow} = require('electron')
, поэтому я не уверен, как проверить, что не так?
Ответ №1:
Оказывается, это предназначено для запуска с помощью команды ‘electron’, поэтому, поскольку я не устанавливал ее глобально (предполагая, что я нахожусь в каталоге моего проекта):
node_modules/.bin/electron ./src/goelectron.js
в противном случае, если бы у меня было, это было бы:
electron ./src/goelectron.js
Комментарии:
1. Вам не следует устанавливать
electron
глобально, вместо этого вы должны добавить скрипт NPM к вашему,package.json
который выполняетсяelectron src/goelectron.js
.2. Предложение о добавлении в package.json полезно — спасибо