安装 electron-builder
npm i electron-builder -D
项目下配置 .npmrc 文件
electron_builder_binaries_mirror=https://registry.npmmirror.com/-/binary/electron-builder-binaries/
注:
electron_builder_binaries_mirror 是 Electron Builder 二进制文件的镜像源
默认下载地址示例:
https://github.com/electron-userland/electron-builder-binaries/releases/download/nsis-3.0.4.1/nsis-3.0.4.1.7z
......
package.json
{
"type": "module",
"name": "demo",
"version": "1.0.0",
"main": "main.js",
"scripts": {
"start": "chcp 65001 && electron .",
"dev": "chcp 65001 && nodemon --exec electron .",
"pack": "electron-builder --dir",
"build": "electron-builder"
},
"keywords": [],
"author": "",
"license": "ISC",
"description": "",
"devDependencies": {
"electron": "^32.0.1",
"electron-builder": "^25.0.5"
}
}
应用打包
构建包含应用程序的文件夹(免安装软件)
npm run pack
构建应用程序
npm run build
注
若运行 npm run pack 或 npm run build 时报错如下:
cannot execute cause=exit status
解决方法:
以管理员身份运行 powershell
切换项目目录
cd D:\workspace\electron\demo
生成包含应用程序的文件夹
npm run pack
解决打包后安装的软件 "系统托盘" 和 "通知" 图标丢失问题
//创建托盘
const createTray = () => {
//const icon = "resource/images/code.ico" //托盘图标
const icon = path.resolve(__dirname,"resource/images/code.ico")
}
//发送通知
ipcMain.handle("sendNotify", async () => {
const notify = new Notification({
//icon:"resource/images/code.ico", //自定义图标
icon:path.resolve(__dirname,"resource/images/code.ico"),
})
})