electron打包前端页面为exe桌面应用

前提

node安装

npm , electron , nativefier 等都需要依赖node.js,所以需要首先安装node.js。node安装可以直接百度搜索node,如图
node
任选一个下载即可,我选的是14.11.0版的。双击下载下来的msi文件,即可进入安装程序,安装程序需要注意安装的路径,最好不要安装在c盘,其余的默认就好,一路next等待安装完毕即可。
安装完node可以在Windows cmd命令行下输入以下命令,如果都出现版本号,如 v10.1.2表明安装成功。

node -v
npm -v

然后需要为node配置环境变量,在我的电脑-》属性-》高级设置-》环境变量中配置,系统变量中新增NODE_PATH,路径选择安装目录下的node_modules即可,如下图
在这里插入图片描述
在node的安装目录中新建两个文件夹node_cache node_global作为npm下载的路径和缓存路径。
在这里插入图片描述

然后配置用户变量path,点击编辑,结果如下图。

在这里插入图片描述
至此node安装完毕。

npm镜像配置等

因为npm是从国外的网站下载,所有接下来需要配置npm镜像。
配置npm依次输入下列命令即可

npm config set registry https://registry.npm.taobao.org/
npm config set perfix D:\node\node_global
npm config set cache D:\node\node_cache
npm config set electron_mirror http://npm.taobao.org/mirrors/electron/

这些命令中perfix和cache配置了npm的全局下载路径和缓存路径,剩下的两条命令配置了npm淘宝镜像。
或者可以下载cnpm,使用cnpm命令下载,不过不推荐,后续打包需要使用npm,但打包过程中需要下载electron-v10.1.2-win32-64.zip文件,如果npm没有配置淘宝镜像,照样很慢,甚至失败。

 npm install -g cnpm --registry=https://registry.npm.taobao.org

至此npm的各种配置完成。

electron打包

打包准备

我这里使用的是最简单的格式,项目目录如下

在这里插入图片描述
hello为项目根目录,如果想要使用electron打包的文件比较复杂,可以先在idea或者其它代码工具中打包,然后替换index.html文件。所以index.html中可以自己定义,这里仅贴出main.js package.json两个文件的代码
main文件

const electron = require('electron');
// Module to control application life.
const {app} = electron;
// Module to create native browser window.
const {BrowserWindow} = electron;
// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let win;
function createWindow() {
  // Create the browser window.
  win = new BrowserWindow({width: 800, height: 600});
   // and load the index.html of the app.
  win.loadURL(`http://localhost:8083/admin/scan/data`);
  // Open the DevTools.
  win.webContents.openDevTools();
  // Emitted when the window is closed.
  win.on('closed', () => {
    // Dereference the window object, usually you would store windows
    // in an array if your app supports multi windows, this is the time
    // when you should delete the corresponding element.
    win = null;
  });
}
// 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.on('ready', createWindow);
// Quit when all windows are closed.
app.on('window-all-closed', () => {
  // On OS X it is common for applications and their menu bar
  // to stay active until the user quits explicitly with Cmd + Q
  if (process.platform !== 'darwin') {
    app.quit();
  }
});
app.on('activate', () => {
  // On OS X 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 (win === null) {
    createWindow();
  }
  });
  // 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.

package文件

{
  "name": "5808",
  "version": "0.1.0",
  "main": "main.js",
  "scripts": {
    "start": "electron .",
    "package":"electron-packager . helloworld --platform=win32 --arch=x64 --electronVersion=10.1.2 --overwrite --ignore=node_modules --ignore=.gitignore"
  },
  "devDependencies": {
    "electron-packager": "^15.1.0"
  }
}

其中package electron-packager 可以根据需要查阅文档进行修改。
mian.js 文件中win.loadURL()用于打包网址,loadFile()可打包文件,根据需要修改。

打包

进入项目根目录,即hello文件夹,输入以下命令即可完成打包

npm install electron-packager --save-dev
npm install electron-packager -g
npm run package

踩过的坑

npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! 5808@0.1.0 package: electron-packager . 5808 --platform=win32 --arch=x64 --electron-version 10.1.2 --overwrite --ignore=node_modules
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the 5808@0.1.0 package script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
报错信息因为package.json中–electron-version 应该改为–electronVersion

然后下载很慢是因为npm没有配置好淘宝镜像,npm config set registry 和 npm config set electron_mirror缺一不可

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Electron 可以将前端项目打包桌面应用程序,具体步骤如下: 1. 配置 package.json 文件 在 package.json 文件中添加以下内容: ``` "main": "main.js", "scripts": { "start": "electron ." }, "dependencies": { "electron": "^10.1.1" } ``` 其中,`main` 指定 Electron 主进程的入口文件,`scripts` 中的 `start` 命令用于启动应用程序,`dependencies` 中指定 Electron 的版本。 2. 创建主进程入口文件 在项目根目录下创建 `main.js` 文件,作为 Electron 主进程的入口文件。在 `main.js` 文件中,可以通过 `BrowserWindow` 类创建一个窗口,并加载前端项目的入口页面。 ```javascript const { app, BrowserWindow } = require('electron') function createWindow () { const win = new BrowserWindow({ width: 800, height: 600, webPreferences: { nodeIntegration: true } }) win.loadFile('index.html') } app.whenReady().then(() => { createWindow() }) ``` 在上述代码中,通过 `BrowserWindow` 类创建了一个窗口,并使用 `loadFile` 方法加载了前端项目的入口页面 `index.html`。 3. 打包应用程序 使用以下命令打包应用程序: ``` electron-packager . MyApp --platform=<platform> --arch=<arch> ``` 其中,`MyApp` 是应用程序的名称,`<platform>` 可以是 `darwin`、`linux` 或 `win32`,表示打包的目标平台,`<arch>` 可以是 `ia32`、`x64` 或 `armv7l`,表示打包的目标架构。打包完成后,会在项目根目录下生成一个与目标平台和架构相关的文件夹,包含了打包后的应用程序。 以上就是使用 Electron前端项目打包的基本步骤。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值