在使用Electron进行开发之前,您需要安装 Node.js。
要检查 Node.js 是否正确安装,请在您的终端输入以下命令:
node -v
npm -v
这两个命令应输出了 Node.js 和 npm 的版本信息。
mkdir my-electron-app && cd my-electron-app
npm init -y
init初始化命令会提示您在项目初始化配置中设置一些值 为本教程的目的,有几条规则需要遵循:
入口点 应当是 main.js (您很快就会创建它)
author、license 和 description可为任意值,但对于 应用打包 是必填项。
{
"name": "my-electron-app",
"version": "1.0.0",
"description": "Hello World!",
"main": "main.js",
"author": "Jane Doe",
"license": "MIT"
}
npm install --save-dev electron
{
"scripts": {
"start": "electron ."
}
}
在您项目的根目录下创建一个名为main.js的文件
const { app, BrowserWindow } = require('electron')
//在第一行中,我们使用 CommonJS 语法导入了两个 Electron 模块:
//app,它着您应用程序的事件生命周期。
//BrowserWindow,它负责创建和管理应用窗口。
const createWindow = () => {
const win = new BrowserWindow({
width: 800,
height: 600
})
win.loadFile('index.html')
}
//在应用准备就绪时调用函数
app.whenReady().then(() => {
createWindow()
})
在您的项目根目录下创建一个名为index.html的文件
Document
hello
npm start
npm install --save-dev @electron-forge/cli
npx electron-forge import
✔ Checking your system
✔ Initializing Git Repository
✔ Writing modified package.json file
✔ Installing dependencies
✔ Writing modified package.json file
✔ Fixing .gitignore
We have ATTEMPTED to convert your app to be in a format that electron-forge understands.
Thanks for using “electron-forge”!!!
package.json
"scripts": {
"start": "electron-forge start",
"package": "electron-forge package",
"make": "electron-forge make"
},
npm run make
[email protected] make /my-electron-app
electron-forge make
✔ Checking your system
✔ Resolving Forge Config
We need to package your application before we can make it
✔ Preparing to Package Application for arch: x64
✔ Preparing native dependencies
✔ Packaging Application
Making for the following targets: zip
✔ Making for target: zip - On platform: darwin - For arch: x64
// Example for macOS
out/
├── out/make/zip/darwin/x64/my-electron-app-darwin-x64-1.0.0.zip
├── …
└── out/my-electron-app-darwin-x64/my-electron-app.app/Contents/MacOS/my-electron-app
npm install electron-builder --save-dev
"scripts": {
"build": "electron-builder",
"build:mac": "electron-builder --mac",
"build:win": "electron-builder --win",
},
mac:icon.icons or icon.png 512 * 512
win:icon.ico or icon.png 256 * 256
linux:icon.png 256 * 256
npm run build
在大多数情况下,这些错误都是由网络问题导致,而不是因为 electron npm 包的问题。 如 ELIFECYCLE、EAI_AGAIN、ECONNRESET 和 ETIMEDOUT 等错误都是此类网络问题的标志。 最佳的解决方法是尝试切换网络,或是稍后再尝试安装。
如果通过 npm 安装失败,你也可以尝试通过从 https://github.com/electron/electron/releases 直接下载 Electron
npm下载electron失败,可以使用yarn来下载electron。先npm install -g yarn,下载yarn,再yarn config set ELECTRON_MIRROR https://npmmirror.com/mirrors/electron/指定electron的镜像,最后再yarn add electron。就大功告成了!
按照错误提示,是 electron-v13.6.9-win32-x64.zip这个文件下载失败,可以利用报错信息中的https://github.com/electron/electron/releases/download/v13.6.9/electron-v13.6.9-win32-x64.zip这个地址进行下载,或者到GitHub上下载。
https://npm.taobao.org/mirrors/electron/26.4.1/electron-v26.4.1-win32-x64.zip
下载后找到以下位置,将压缩包解压到electron的cache文件夹中。
C:\Users\admin\AppData\Local\electron\Cache
https://registry.npmmirror.com/binary.html?path=electron-builder-binaries/winCodeSign-2.6.0/winCodeSign-2.6.0.7z
下载后找到以下位置,将压缩包解压到electron-builder的cache文件夹中。
C:\Users\admin\AppData\Local\electron-builder\Cache
https://registry.npmmirror.com/binary.html?path=electron-builder-binaries/
C:\Users\admin\AppData\Local\electron-builder\Cache\nsis
这里有你需要的依赖文件可以手动下载
web 站点
https://npmmirror.com/
二进制文件镜像
https://npm.taobao.org/mirrors/
https://registry.npmmirror.com/binary.html
npm install -g cnpm --registry=https://registry.npm.taobao.org
npm install -g cnpm --registry=https://registry.npmmirror.com
npm config set registry https://registry.npm.taobao.org
npm config set registry https://registry.npmmirror.com
npm config get registry
cnpm config get registry