一、node版本匹配
首页会有这样一段: 也就是node至少需要12.18.3才可以使用。目前官网推荐node 14.15.1。
二、安卓cnpm
npm i cnpm -g
二、开发环境安装
新建文件夹 testapp
cd testapp
cnpm i electron -g
创建index.js
var electron = require('electron');
var app = electron.app;
var BrowserWindow = electron.BrowserWindow;
var mainWindow = null; //打开的窗口
//console.log(require('electron').remote)
const { dialog } = require('electron')
const Menu = electron.Menu;
app.on('ready',()=>{
//console.log()
Menu.setApplicationMenu(null)
mainWindow = new BrowserWindow({width:1000,height:750,webPreferences:{enableRemoteModule:true,nodeIntegration:true}});
mainWindow.loadFile('index.html');//加载网页
mainWindow.on('closed',()=>{
mainWindow = null;
});
})
创建index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
<title>Document</title>
</head>
<body>
hi litter brother
</body>
</html>
命令行执行
npm init --yes
electron .
就可以看到效果了
三、打包环境安装
编辑package.json
完善description和author信息后。
命令行运行,安装打包环境
npx @electron-forge/cli import
命令行运行,打包exe
npm run make
四、常见问题
1.node版本问题,见第一节。
2.渲染程序无法使用remote,参考第二节 enableRemoteModule:true。