electron 安装使用

Electron应用搭建教程
本文详细介绍了使用Node.js和Electron构建跨平台桌面应用程序的过程,包括Node.js与npm的安装配置、Electron及其相关工具的安装、项目创建及运行等步骤。

 

1.安装 node.js

链接:http://pan.baidu.com/s/1o7W7BIy 密码:y6od

   一路next

  我安装在F:\Program Files\node.js

2.检查node.js和npm是否安装成功

  1. 命令行:node -v
  2. 命令行:npm -v

 

 

建议把npm的仓库切换到国内taobao仓库,

注册cnpm命令,如下

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

 

 

 

 

 

 

3. Electron的安装

cnpm install -g electron

 

 

electron是否安装成功可通过命令 electron -v 查看。

 

 

 

 

4. 打包输出工具

cnpm install -g electron-packager

 

 

 

 

5.electron 客户端工具

Electron.exe

链接:http://pan.baidu.com/s/1mieJnLI 密码:x2i8

安装完成双击electron.exe文件

 

 6.新建一个程序

新建一个文件夹,命名为electron,在文件夹目录下,创建三个文件,package.jsonmain.jsindex.html

 

文件夹目录的结构如下:

 

这里写图片描述

 

package.json

 

1 {
2   "name"    : "your-app",
3   "version" : "0.1.0",
4   "main"    : "main.js"
5 }
6 
7  

 

 

main.js

 

 1 const electron = require('electron');
 2 // Module to control application life.
 3 const {app} = electron;
 4 // Module to create native browser window.
 5 const {BrowserWindow} = electron;
 6 
 7 // Keep a global reference of the window object, if you don't, the window will
 8 // be closed automatically when the JavaScript object is garbage collected.
 9 let win;
10 
11 function createWindow() {
12   // Create the browser window.
13   win = new BrowserWindow({width: 800, height: 600});
14 
15   // and load the index.html of the app.
16   win.loadURL(`file://${__dirname}/index.html`);
17 
18   // Open the DevTools.
19   win.webContents.openDevTools();
20 
21   // Emitted when the window is closed.
22   win.on('closed', () => {
23     // Dereference the window object, usually you would store windows
24     // in an array if your app supports multi windows, this is the time
25     // when you should delete the corresponding element.
26     win = null;
27   });
28 }
29 
30 // This method will be called when Electron has finished
31 // initialization and is ready to create browser windows.
32 // Some APIs can only be used after this event occurs.
33 app.on('ready', createWindow);
34 
35 // Quit when all windows are closed.
36 app.on('window-all-closed', () => {
37   // On OS X it is common for applications and their menu bar
38   // to stay active until the user quits explicitly with Cmd + Q
39   if (process.platform !== 'darwin') {
40     app.quit();
41   }
42 });
43 
44 app.on('activate', () => {
45   // On OS X it's common to re-create a window in the app when the
46   // dock icon is clicked and there are no other windows open.
47   if (win === null) {
48     createWindow();
49   }
50 });
51 
52 // In this file you can include the rest of your app's specific main process
53 // code. You can also put them in separate files and require them here.

 

 

 

 

 

index.html

 

 1 <!DOCTYPE html>
 2 <html>
 3   <head>
 4     <meta charset="UTF-8">
 5     <title>Hello World!</title>
 6   </head>
 7   <body>
 8     <h1>Hello World!</h1>
 9     We are using node <script>document.write(process.versions.node)</script>,
10     Chrome <script>document.write(process.versions.chrome)</script>,
11     and Electron <script>document.write(process.versions.electron)</script>.
12   </body>
13 </html>

 

 

 

 

 

 

7.运行程序

 

两种方法

1.命令行

cmd里面写

 

 

 

Electron.exe安装路径

 

 

 

 

 

自己的文件夹的路径

 

 

 

 

 

 

 

2.

 把你写的文件夹直接拖到electron.exe里面。

 

 

 

转载于:https://www.cnblogs.com/tanyongli/p/7504603.html

### Electron 安装报错解决方案 在安装 Electron 时,如果遇到错误 `Error: Electron failed to install correctly, please delete node_modules/electron and try installing again`,可以按照以下方法解决。 #### 1. 删除已损坏的 Electron 文件 首先需要删除项目中的 `node_modules/electron` 目录,以确保重新安装时不会受到缓存或损坏文件的影响。可以通过以下命令完成: ```bash rm -rf node_modules/electron ``` 随后重新运行安装命令: ```bash npm install electron --save-dev ``` 此步骤有助于清除可能存在的损坏安装文件[^1]。 #### 2. 检查网络连接与镜像源配置 如果上述方法仍然失败,可能是由于网络问题导致下载中断。建议使用国内的 npm 镜像源来加速 Electron安装过程。可以在项目的根目录下创建或编辑 `.npmrc` 文件,并添加以下内容: ``` ELECTRON_MIRROR=https://npmmirror.com/mirrors/electron/ ELECTRON_BUILDER_BINARIES_MIRROR=https://npmmirror.com/mirrors/electron-builder-binaries/ ``` 通过配置镜像源,能够有效避免因网络不稳定引起的下载失败问题[^4]。 #### 3. 确保 Node.js npm 版本兼容 Electron 对 Node.js npm 的版本有一定要求。如果使用的 Node.js 或 npm 版本过低,可能会导致安装失败。可以通过以下命令检查当前版本: ```bash node -v npm -v ``` 如果版本不满足需求,可以升级到最新稳定版。推荐使用 [nvm](https://github.com/nvm-sh/nvm) 来管理 Node.js 版本。 #### 4. 使用淘宝镜像源安装 作为另一种选择,可以尝试使用淘宝提供的 npm 镜像源进行安装。执行以下命令: ```bash npm install electron --save-dev --registry=https://registry.npmmirror.com ``` 这将临时切换到淘宝镜像源,从而提高下载速度成功率[^2]。 #### 5. 检查系统环境变量 某些情况下,系统环境变量配置不当也可能导致安装失败。确保 PATH 中包含 Node.js npm 的安装路径。例如,在 Windows 系统中,可以将以下路径添加到环境变量中: ``` C:\Program Files\nodejs\ ``` #### 6. 处理特殊场景(如中文路径) 如果电脑账户名或项目路径中包含中文字符,可能会导致打包或安装过程中出现乱码问题。建议将项目移动到纯英文路径下重新尝试安装。此外,还可以通过修改相关配置文件解决编码问题[^5]。 --- ### 示例代码 以下是一个简单的脚本,用于检查修复 Electron 安装问题: ```bash #!/bin/bash # 删除损坏的 Electron 文件 if [ -d "node_modules/electron" ]; then rm -rf node_modules/electron fi # 配置镜像源 echo "ELECTRON_MIRROR=https://npmmirror.com/mirrors/electron/" >> .npmrc echo "ELECTRON_BUILDER_BINARIES_MIRROR=https://npmmirror.com/mirrors/electron-builder-binaries/" >> .npmrc # 重新安装 Electron npm install electron --save-dev ``` --- ### 注意事项 - 如果问题仍未解决,可以尝试全局安装 Electron 并测试其功能: ```bash npm install -g electron ``` - 在排查问题时,建议查看详细的日志信息,以便定位具体原因。可以使用以下命令启用调试模式: ```bash DEBUG=electron* npm install electron ``` ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值