当前位置: 首页 > news >正文

electron打包基本教程

从0开始搭建

  • 概要
  • 步骤
    • 基础软件
    • 运行项目
    • 打包项目
  • 注意事项

概要

将html打包成桌面的主流有electron和nwjs,nwjs更加简单,但是使用效果不如electron,electron打包比较麻烦,但是效果比较好,反正各有优势和缺点

步骤

基础软件

  1. nodejs
    官网下载
    阿里下载
# 验证版本
node -v
v22.13.0
  1. npm
    nodejs自带npm,直接查看版本
npm -v
10.9.2
  1. cnpm
    国内用户需安装这个,你懂的
#使用 npm 全局安装 cnpm
npm install -g cnpm --registry=https://registry.npm.taobao.org
  1. 生成项目
  • 创建目录 my-electron-app
  • 进入目录,命令创建
cnpm init -y
# -y 参数表示使用默认配置快速初始化,免去手动回答一系列问题的步骤。
  1. 添加依赖
# 用于开发
cnpm install electron --save-dev
# 用于打包
cnpm install electron-builder --save-dev
  1. 指定国内地址
    打包时会到github下载很多文件,如果连接github不顺畅,可以指定国内地址
  "build": {"electronDownload": {"mirror": "https://registry.npmmirror.com/-/binary/electron/"},"appId": "com.cn.app","mac": {"target": "dmg"},"win": {"target": "nsis"},"linux": {"target": "AppImage"}},
  1. 完整项目结构
    在这里插入图片描述
  • app:需要打包的html网站,这里测试添加了一个简单的html单文件
  • dist:打包后文件目录
  • node_modules:node依赖包,自动生成的
  • main.js:项目主入口,package.json中指定
  • package.json:项目结构

main.js

const { app, BrowserWindow } = require('electron');
const path = require('path');function createWindow() {const win = new BrowserWindow({width: 800,height: 600,webPreferences: {nodeIntegration: true}});// win.loadURL('https://github.com');win.loadFile(path.join(__dirname, 'app', 'index.html'));
}app.whenReady().then(createWindow);app.on('window-all-closed', () => {if (process.platform !== 'darwin') {app.quit();}
});app.on('activate', () => {if (BrowserWindow.getAllWindows().length === 0) {createWindow();}
});

package.json

{"name": "my-electron-app","version": "1.0.0","description": "","main": "main.js","scripts": {"test": "echo \"Error: no test specified\" && exit 1","start": "electron .","build": "electron-builder"},"build": {"electronDownload": {"mirror": "https://registry.npmmirror.com/-/binary/electron/"},"appId": "com.cn.app","mac": {"target": "dmg"},"win": {"target": "nsis"},"linux": {"target": "AppImage"}},"keywords": [],"author": "","license": "ISC","devDependencies": {"electron": "^34.2.0","electron-builder": "^25.1.8"}
}

运行项目

# cmd命令
cnpm run start

运行后出现浏览器,里面是app包含的网站
在这里插入图片描述

打包项目

cnpm run build
  • 打包中会出现访问winCodeSign(github)失败,这个主要用于软件签名,防止软件被篡改,如果签名失败也会打包项目,但是项目名字是win-unpacked,意思未签名打包目录
  • 打开win-unpacked
  • 在这里插入图片描述

注意事项

  1. 远程打包失败,可以下载electron到本地,指定本地打包
    https://npmmirror.com/mirrors/electron/
    更多方法豆包[electron国内下载打包方法]
http://www.lryc.cn/news/538826.html

相关文章:

  • 实现pytorch注意力机制-one demo
  • 深入Flask:如何优雅地处理HTTP请求与响应
  • JVM ②-双亲委派模型 || 垃圾回收GC
  • jQuery介绍(快速、简洁JavaScript库,诞生于2006年,主要目标是简化HTML文档操作、事件处理、动画和Ajax交互)
  • python旅游推荐系统+爬虫+可视化(协同过滤算法)
  • Ubuntu 22.04.5 LTS 安装企业微信,(2025-02-17安装可行)
  • 【Excel笔记_6】条件格式和自定义格式设置表中数值超过100保留1位,超过1000保留0位,低于100为默认
  • UDP与TCP
  • Web开发技术概述
  • 解压rar格式的软件有哪些?8种方法(Win/Mac/手机/网页端)
  • uniapp开发:首次进入 App 弹出隐私协议窗口
  • 执行pnpm run dev报错:node:events:491 throw er; // Unhandled ‘error‘ event的解决方案
  • OpenCV机器学习(4)k-近邻算法(k-Nearest Neighbors, KNN)cv::ml::KNearest类
  • JVM中的线程池详解:原理→实践
  • SNARKs 和 UTXO链的未来
  • JavaScript设计模式 -- 外观模式
  • 百达翡丽(Patek Philippe):瑞士制表的巅峰之作(中英双语)
  • 阿里云一键部署DeepSeek-V3、DeepSeek-R1模型
  • 分享一款AI绘画图片展示和分享的小程序
  • 【练习】【双指针】力扣热题100 283. 移动零
  • QT 互斥锁
  • 什么是算法的空间复杂度和时间复杂度,分别怎么衡量。
  • VMware Workstation 17.0 Pro创建虚拟机并安装Ubuntu22.04与ubuntu20.04(双版本同时存在)《包含小问题总结》
  • Windows 10 ARM工控主板CAN总线实时性能测试
  • 如何在不依赖函数调用功能的情况下结合工具与大型语言模型
  • 【Linux AnolisOS】关于Docker的一系列问题。尤其是拉取东西时的网络问题,镜像源问题。
  • 【Elasticsearch】Mapping概述
  • GPT-4o悄然升级:能力与个性双突破,AI竞技场再掀波澜
  • 如何选择合适的超参数来训练Bert和TextCNN模型?
  • C# SpinLock 类 使用详解