全局安装
cnpm install electron -g
检查是否安装成功
electron -v
如果失败,则全局安装 (window10,64 位系统)
cnpm install electron-prebuilt -g
创建项目
目录结构
index.html
随便写
main.js
var electron = require('electron')
var app = electron.app
var BrowserWindow = electron.BrowserWindow
var mainWindow = null
// 监听应用准备完成
app.on('ready', function() {
mainWindow = new BrowserWindow({
width: 800,
height: 800
})
// mainWindow.loadFile('index.html') // 报错
// 打开文件
mainWindow.loadURL('file://' + __dirname + '/index.html')
mainWindow.on('closed', () => {mainWindow = null})
})
// 监听所有窗口关闭事件
app.on('window-all-closed', function() {if (process.platform !== 'darwin') {app.quit()
}
})
package.json
main 为主入口文件
{
"name": "electron02",
"version": "1.0.0",
"description": "","main":"main.js","scripts": {"test":"echo \"Error: no test specified\" && exit 1"},"author":"",
"license": "ISC",
"dependencies": {"electron": "^7.0.0"}
}