vue3项目使用Electron打包成exe的方法与打包报错解决

将vue3项目打包成exe文件方法

一、安装

1.安装electron

复制代码
npm install electron --save-dev

npm install electron-builder --save-dev

2.在vue项目根目录新建文件index.js

javascript 复制代码
// index.js

// Modules to control application life and create native browser window
const { app, BrowserWindow } = require('electron')

const createWindow = () => {
  // Create the browser window.
  const mainWindow = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
      nodeIntegration: true
    }
  })

  // 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', () => {
    // 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', () => {
  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.

3.package.json文件编辑

javascript 复制代码
"scripts": {

    "serve": "vue-cli-service serve",

    "build": "vue-cli-service build",

    "lint": "vue-cli-service lint",

    "electron:build": "vue-cli-service build && electron-builder",

    "electron:serve": "electron ."

  },



  "build": {

    "productName": "这里是你的项目名",

    "appId": "com.example.这里是appId",

    "win": {

      "icon": "favicon.ico",

      "target": ["nsis", "appx"]

    },



    "directories": {

      "output": "build"

    },

    "files": [

      "dist/**/*",

      "index.js"//这里是刚才建的index.js

    ]

  },

4.测试

javascript 复制代码
npm run electron:serve

5.打包

javascript 复制代码
npm run electron:build

二、报错解决

解决:打开cmd 执行 npm config edit

javascript 复制代码
npm config edit

打开配置文件 粘贴以下内容

javascript 复制代码
registry=https://registry.npm.taobao.org/
sass_binary_site=https://npm.taobao.org/mirrors/node-sass/
phantomjs_cdnurl=http://npm.taobao.org/mirrors/phantomjs
electron_mirror=https://npm.taobao.org/mirrors/electron/
ELECTRON_MIRROR=http://npm.taobao.org/mirrors/electron/
相关推荐
digger_4196 小时前
从零理解 Electron:主进程、渲染进程、preload 与 IPC 实战
前端·electron
snow@li8 小时前
前端:导入EXCEL并解析文件内容,demo
前端·javascript·vue.js
RuoyiOffice9 小时前
Flowable只是起点:一套OA背后的单据、资源与协作设计
java·vue.js·spring boot·flowable·企业管理·oa·ruoyi office
夕水11 小时前
一个小小的颜色选择器,差点让我开发出一个组件库来
前端·javascript·typescript
时间的情敌12 小时前
Three.js 射线拾取进阶:hover 高亮、点击弹窗、多物体过滤、穿透拾取
开发语言·javascript·three.js
光影少年13 小时前
ts全栈开发
javascript·后端·typescript·node.js·c5全栈
bai27313 小时前
主线程里 foo 和 FOO 是同一个变量,Worker 里就成了两个:Windows 上 process.env 的大小写边界
javascript·后端
code2cat14 小时前
React入门之任务列表:交换两行后,展开的详情跟着谁走
前端·javascript·react.js
map_3d_vis15 小时前
JSAPI 4.0 默认行为一览:滚轮缩放、默认视野、缩放范围和 addLayer
前端·javascript·地图·百度地图
FanetheDivine1 天前
总结近期关于Agent的思考和成果
javascript·agent·ai编程