1.安装electron
npm一直失败,我就cnpm安装的
2.创建一个简单示例:
新建一个文件夹,然后cmd
先搞个package.json:
npm init -y
然后再添加electron依赖
cnpm install electron
然后再写个main.js
const { app, BrowserWindow, Menu } = require('electron')function createWindow() {const win = new BrowserWindow({width: 800,height: 600,webPreferences: {nodeIntegration: true}})win.loadURL('http://baidu.com')win.webContents.on('dom-ready', () => {win.webContents.executeJavaScript(``);})
}//Menu.setApplicationMenu(null);app.whenReady().then(createWindow)
然后这个文件夹中一共就三项:
package.json
main.js
node_modules
然后配置一下package.json::
{"name": "666","version": "1.0.0","description": "","main": "main.js","scripts": {"start": "electron .","packager": "electron-packager . fukaiitapp --out fukaiitapp --arch=x64 --overwrite --ignore=node_modules ",},
然后如果要跑起来:
npm run start
如果要打包成exe:(具体配置在package。json中的 “packager”: "electron-packager . fukaiitapp --out fukaiitapp --arch=x64 --overwrite --ignore=node_module --electron-zip-dir= …/electron-v20.1.1-win32-x64.zip)
npm run packager
3.配置环境或者打包如果出现网络连接问题(timeout):
在文件夹里新建一个.npmrc文件,文件内容写:
electron_mirror=https://npmmirror.com/mirrors/electron/
就可以打包成功了