用electron将vue项目打包成.exe文件【保姆级教程】

用electron将vue项目打包成.exe文件【保姆级教程】

说明: vue2项目,使用的vue-element-admin框架,用electron打包成.exe文件。

1、新建一个文件夹,然后右键打开终端或者cd到这个目录。执行下面的命令(最好确认一下github网站是否登陆了)。

javascript 复制代码
//将electron官网中的quick-start拉取到本地
git clone https://github.com/electron/electron-quick-start


安装成功的文件如下显示:

2.安装好后,用vscode(其他类似工具)打开,新建终端,前后输入下面的命令。

javascript 复制代码
//下载项目需要的依赖
npm install

//安装打包.exe所需要的依赖electron-packager、electron
npm i electron-packager --save-dev
npm i electron --save-dev 

//运行看看是否可以成功
npm run start

如果运行成功,会弹出一个页面,如下图:


3、删除electron-quick-start文件中的index.html,把自己的项目dist文件放进去。

4、打开electron-quick-start文件里的main.js文件,下面是mian.js完整代码。
重点是: mainWindow.loadFile("./dist/index.html") 的修改

javascript 复制代码
// Modules to control application life and create native browser window
const { app, BrowserWindow } = require("electron");
const path = require("node:path");

function createWindow() {
  // Create the browser window.
  const mainWindow = new BrowserWindow({
    width: 1024, //自己需要的宽高
    height: 960,
    webPreferences: {
      preload: path.join(__dirname, "preload.js"),
    },
  });

  // and load the index.html of the app.
  mainWindow.loadFile("./dist/index.html");
  // Open the DevTools.
  mainWindow.webContents.openDevTools(); // 打开调试工具
}

// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.whenReady().then(() => {
  createWindow();

  app.on("activate", function () {
    // On macOS it's common to re-create a window in the app when the
    // dock icon is clicked and there are no other windows open.
    if (BrowserWindow.getAllWindows().length === 0) createWindow();
  });
});

// Quit when all windows are closed, except on macOS. There, it's common
// for applications and their menu bar to stay active until the user quits
// explicitly with Cmd + Q.
app.on("window-all-closed", function () {
  if (process.platform !== "darwin") app.quit();
});

// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and require them here.

5、打开 package.json文件,添加上有关packager的代码。

(PS: 如果不需要自动匹配dist文件夹下的应用图标使用该内容

"electron-packager ./ Vite App --platform=win32 --arch=x64 --overwrite")

javascript 复制代码
  "scripts": {
    "start": "electron .",
    "packager": "electron-packager ./ 你想用的exe名字 --platform=win32 --arch=x64 --icon=./dist/favicon.ico --overwrite"
  },


6、终于到了最后一步:npm run packager进行打包!打包成功后如下图

javascript 复制代码
npm run packager
相关推荐
赤生云电竞13 小时前
网吧无盘系统上云:PXE 与云桌面混合部署实践 —— 基于iPXE、iSCSI与NVIDIA vGPU的架构选型
架构·github
爱嘿嘿的小黑13 小时前
用 Google Antigravity 踩遍登录和环境坑后,我开源了个 30 秒只读体检脚本 + Agent Skill
人工智能·python·github
逛逛GitHub13 小时前
7 天 Star 增长超 1000 的新 GitHub 项目,这一波质量很高。
github
CoderYanger14 小时前
Java EE 进阶:16. Spring原理(第一弹)
java·服务器·数据库·后端·spring·java-ee·github
北城笑笑17 小时前
Vue 115 ,上篇:给智能体装上计费表,行知订阅次数额度与每日额度双规则设计( 需求分析与方案设计,Vue3 + RuoYi 3.9.2 + 主子账号 )
java·前端·javascript·数据库·vue.js·后端·vue
徐小夕19 小时前
JitKnow 5.0发布:让AI知识库变成可组装的知识中台
前端·vue.js·github
钱栈up1 天前
GitHub 热榜实测|sherlock 环境部署实录
python·开源·github
粥里有勺糖1 天前
视野修炼-技术周刊第139期 | AI 不会让时间变多
前端·github·ai编程
一点一木1 天前
🚀 2026 年 11 月 GitHub 十大热门项目排行榜 🔥
人工智能·github
OpenTiny社区2 天前
当 AI 开始写 UI,我们缺的不是模型,是"设计词汇"
前端·github·ai编程