一、Electron是什么?
Electron是一個使用 JavaScript、HTML 和 CSS 構建桌面應用程序的框架。 嵌入?Chromium?和?Node.js?到 二進制的 Electron 。允許您保持一個 JavaScript 代碼代碼庫并創建可在Windows、macOS和Linux上運行的跨平臺應用 。
Electron 經常與 Chromium 一起發布主版本。 本文檔側重于發布排期和版本支持政策。 要更深入地了解我們的git 分支和Electron如何使用語義版本, 請查看我們的?Electron 版本文檔。?
安裝Electron時注意要選擇使用其支持的node版本的開發環境。
時間表?
Electron | Alpha | 測試版 | 穩定版 | EOL | Chrome | Node | 支持 |
---|---|---|---|---|---|---|---|
29.0.0 | 2023-Dec-07 | 2024年3月24日 | 2024年2月20日 | 2024年8月20日 | M122 | v18.19 | ? |
28.0.0 | 2023年11月10日 | 2023年11月06日 | 2023年12月5日 | 2024年6月11日 | M120 | v18.18 | ? |
27.0.0 | 2023年8月17日 | 2023年9月13日 | 2023年10月10日 | 2024年4月16日 | M118 | v18.17 | ? |
26.0.0 | 2023年6月01日 | 2023年6月27日 | 2023年8月15日 | 2024年2月20日 | M116 | v18.16 | ? |
25.0.0 | 2023年4月10日 | 2023年5月02日 | 2023年5月30日 | 2023年12月5日 | M114 | v18.15 | 🚫 |
24.0.0 | 2023年2月09日 | 2023年3月07日 | 2023年4月04日 | 2023年10月10日 | M112 | v18.14 | 🚫 |
23.0.0 | 2022年12月1日 | 2023年3月10日 | 2023年2月7日 | 2023年8月15日 | M110 | v18.12 | 🚫 |
22.0.0 | 2022年9月29日 | 2022年10月25日 | 2022年11月29日 | 2023年10月10日 | M108 | v16.17 | 🚫 |
21.0.0 | 2022年8月4日 | 2022年8月30日 | 2022年9月27日 | 2023年4月04日 | M106 | v16.16 | 🚫 |
20.0.0 | 2022年5月26日 | 2022年6月21日 | 2022年8月2日 | 2023年2月7日 | M104 | v16.15 | 🚫 |
19.0.0 | 2022年3月31日 | 2022年4月26日 | 2022年5月24日 | 2022年11月29日 | M102 | v16.14 | 🚫 |
18.0.0 | 2022年2月3日 | 2022年3月3日 | 2022年3月29日 | 2022年9月27日 | M100 | v16.13 | 🚫 |
17.0.0 | 2021年11月18日 | 2022年1月6日 | 2022年2月1日 | 2022年8月2日 | M98 | v16.13 | 🚫 |
16.0.0 | 2021年9月23日 | 2021年10月20日 | 2021年11月16日 | 2022年5月24日 | M96 | v16.9 | 🚫 |
15.0.0 | 2021年7月20日 | 2021年9月01日 | 2021年9月21日 | 2022年5月24日 | M94 | v16.5 | 🚫 |
14.0.0 | -- | 2021年5月27日 | 2021年8月31日 | 2022年3月29日 | M93 | v14.17 | 🚫 |
13.0.0 | -- | 2021年3月4日 | 2021年5月25日 | 2022年2月1日 | M91 | v14.16 | 🚫 |
12.0.0 | -- | 2020年11月19日 | 2021年3月2日 | 2021年11月16日 | M89 | v14.16 | 🚫 |
11.0.0 | -- | 2020年8月27日 | 2020年11月17日 | 2021年8月31日 | M87 | v12.18 | 🚫 |
10.0.0 | -- | 2020年5月21日 | 2020年8月25日 | 2021年5月25日 | M85 | v12.16 | 🚫 |
9.0.0 | -- | 2020年2月6日 | 2020年5月19日 | 2021年3月2日 | M83 | v12.14 | 🚫 |
8.0.0 | -- | 2019年10月24日 | 2020年2月4日 | 2020年11月17日 | M80 | v12.13 | 🚫 |
7.0.0 | -- | 2019年8月1日 | 2019年10月22日 | 2020年8月25日 | M78 | v12.8 | 🚫 |
6.0.0 | -- | 2019年4月25日 | 2019年7月30日 | 2020年5月19日 | M76 | v12.14.0 | 🚫 |
5.0.0 | -- | 2019年3月22日 | 2019年4月23日 | 2020年2月4日 | M73 | v12.0 | 🚫 |
4.0.0 | -- | 2018年10月11日 | 2018年2月1日 | 2019年10月22日 | M69 | v10.11 | 🚫 |
3.0.0 | -- | 2018年6月21日 | 2018年9月18日 | 2019年7月30日 | M66 | v10.2 | 🚫 |
2.0.0 | -- | 2018年2月1日 | 2018年5月01日 | 2019年4月23日 | M61 | v8.9 | 🚫 |
二、?解決npm淘寶鏡像到期問題?
報錯信息:
Could not retrieve?https://npm.taobao.org/mirrors/node/latest/SHASUMS256.txt.
Get "https://npm.taobao.org/mirrors/node/latest/SHASUMS256.txt": x509: certificate has expired or is not yet valid:
解決方法:?
<1>.如果使用nvm做node的版本管理 ,則也需要更換nvm倉庫鏡像地址,更換方法如下:
node_mirror: https://npmmirror.com/mirrors/node/
npm_mirror: https://npmmirror.com/mirrors/npm/
?<2>.切換npm鏡像倉庫--如果你是將npm官方鏡像改成了淘寶鏡像,則需要按下面步驟去手動更新。
// 1. 清空緩存
npm cache clean --force// 2. 切換新源
npm config set registry https://registry.npmmirror.com// 3. 檢測是否切換成功
npm config get registry
<3>.更換成cnpm淘寶命令工具cnpm的使用方式:
// 1. 安裝cnpm
npm install -g cnpm --registry=https://registry.npmmirror.com// 2. 檢測是否安裝成功
cnpm -v
緩存位置
window下的緩存位置(npm全局位置):
user\xxx\AppData\Roaming\npm-cache當中的_cacache文件夾
清理緩存?
// 強制刪除緩存
npm cache clean -f
// 無論在項目中執行命令,還是在全局執行命令
// 都會去刪除當前npm的所有緩存內容,即是刪除_cacache文件夾
?應用場景
// 有時npm install因為緩存導致的報錯,需按如下步驟操作
1、刪除node_module文件夾
2、npm cache clear -f
3、npm instaall
三、npm安裝Electron 項目失敗報錯問題和解決辦法?
在這一步就跟定住了一樣,一動不動,時間長了就報錯?
?解決辦法
1、打開npm的配置文件
npm config edit
?2、在打開的配置文件空白處將下面幾個配置添加上去,注意如果有原有的這幾項配置,就修改
registry=https://registry.npmmirror.com electron_mirror=https://cdn.npmmirror.com/binaries/electron/ electron_builder_binaries_mirror=https://npmmirror.com/mirrors/electron-builder-binaries/
或者
注:在安裝 electron 的過程中會根據開發者的當前的操作系統去下載 electron 的二進制包,在下載過程中由于網絡的原因會導致下載失敗,對應的解決方案是指定國內的鏡像,如淘寶鏡像。
#指定 npm 倉庫地址
??npm config set registry https://registry.np mmirror . com?
#指定 electron 二進制包的鏡像地址
??npm config set ELECTRON _ MIRROR -"https://npmmirror.con/mirrors/electron/"
檢測 npm 倉庫和 electron 鏡像地址是否設置成功
??npm config list?
# lotjol @ localhost in ~[12:14:17]
?npm config list?
" user " config from / Users / lotjol /. npmrc?
?ELECTRON _ MIRROR ="https://npmmirror.com/mirrors/electron/"
?home ="https://npm.taobao.org"
?registry ="https://registry.np mmirror . com /"
?3、然后關閉該窗口,重啟命令行,刪除node_modules文件夾,并執行下面命令清除緩存,再重新安裝依賴
npm cache clean --force
4.安裝electron測試
四、安裝 electron-builder報錯解決辦法
報錯原因:
由于 vue add?之前使用的淘寶源,現在淘寶源證書到期,所以報錯。
解決辦法:?
修改 vue 的設置,在c盤用戶目錄下的 vue.rc 文件中修改為false,使得之后的vue add 不再走淘寶鏡像即可。
五、vue2結合electron開發跨平臺應用(桌面端應用)
1.確定nodejs和electron的版本號??
確定nodejs和electron的版本號及其重要,因為electron的開發版本需要指定的nodejs版本支持。?
?本文安裝測試使用的是:
? ?1.node18.19.0
? ?2.npm10.2.3?
? ?3.vue-cli5.0.8
? ?4.electron29.0.0
??
2.創建vue2項目?
vue create elctron29.0.0_node18.19.0_vuecli5.0.8_vue2
?
3.安裝electron?
npm install electron@29.0.0 --save-dev
?
?4.安裝electron-builder
vue add electron-builder
?在項目的src目錄下增加了文件,并在注入
5.啟動electron項目?
npm run electron:serve
?
首次啟動會加載相當緩慢,會有如下提示信息:?
Failed to fetch extension, trying 4 more times
Failed to fetch extension, trying 3 more times
Failed to fetch extension, trying 2 more times
Failed to fetch extension, trying 1 more times
Failed to fetch extension, trying 0 more times
?解決首次加載緩慢的辦法:
? ?在項目中找到background.js文件,注釋background.js文件中的這段代碼:import? ? ? ?installExtension, { VUEJS_DEVTOOLS } from 'electron-devtools-installer'
?再次啟動測試
很快就啟動成功了!?
6.打包electron項目
npm run electron:build
使用?electron-builder 構建 vue 項目,運行 npm run electron:build ,構建過程報錯。這個錯誤可能是由于使用的 Node.js 版本過高,不支持當前的 OpenSSL 版本,導致構建失敗。解決這個問題的方法是在構建命令前設置 NODE_OPTIONS 環境變量,強制使用 OpenSSL 的舊版提供程序。?
報錯提示
C:\Users\63002\Desktop\elctron29.0.0_node18.19.0_vuecli5.0.8_vue2>npm run electron:build> elctron29.0.0_node18.19.0_vuecli5.0.8_vue2@0.1.0 electron:build
> vue-cli-service electron:build?INFO ?Bundling render process:
All browser targets in the browserslist configuration have supported ES module.
Therefore we don't build two separate bundles for differential loading.
- ?Building for production...?DONE ?Compiled successfully in 4261ms ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?13:26:43
? File ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?Size ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Gzipped? dist_electron\bundled\js\chunk-vendors.91055014.js ? ?127.60 KiB ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 43.44 KiB
? dist_electron\bundled\js\app.60bf7bcd.js ? ? ? ? ? ? ?15.53 KiB ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?9.52 KiB
? dist_electron\bundled\js\about.85649f72.js ? ? ? ? ? ?0.50 KiB ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 0.32 KiB
? dist_electron\bundled\css\app.bc18c568.css ? ? ? ? ? ?0.42 KiB ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 0.26 KiB? Images and other types of assets omitted.
? Build at: 2024-03-02T05:26:43.958Z - Hash: 1ffa631b94053caa - Time: 4261ms?DONE ?Build complete. The dist_electron\bundled directory is ready to be deployed.
?INFO ?Check out deployment instructions at https://cli.vuejs.org/guide/deployment.html/ ?Bundling main process...
?ERROR ?Failed to compile with 1 errors ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 13:26:45
?error
background.js from Terser
Error: error:0308010C:digital envelope routines::unsupported
? ? at new Hash (node:internal/crypto/hash:69:19)
? ? at Object.createHash (node:crypto:133:10)
? ? at C:\Users\63002\Desktop\elctron29.0.0_node18.19.0_vuecli5.0.8_vue2\node_modules\vue-cli-plugin-electron-builder\node_modules\webpack\node_modules\terser-webpack-plugin\dist\index.js:217:37
? ? at Array.forEach (<anonymous>)
? ? at TerserPlugin.optimizeFn (C:\Users\63002\Desktop\elctron29.0.0_node18.19.0_vuecli5.0.8_vue2\node_modules\vue-cli-plugin-electron-builder\node_modules\webpack\node_modules\terser-webpack-plugin\dist\index.js:160:259)
? ? at _next0 (eval at create (C:\Users\63002\Desktop\elctron29.0.0_node18.19.0_vuecli5.0.8_vue2\node_modules\vue-cli-plugin-electron-builder\node_modules\tapable\lib\HookCodeFactory.js:33:10), <anonymous>:8:1)
? ? at eval (eval at create (C:\Users\63002\Desktop\elctron29.0.0_node18.19.0_vuecli5.0.8_vue2\node_modules\vue-cli-plugin-electron-builder\node_modules\tapable\lib\HookCodeFactory.js:33:10), <anonymous>:23:1)
? ? at processTicksAndRejections (node:internal/process/task_queues:95:5)C:\Users\63002\Desktop\elctron29.0.0_node18.19.0_vuecli5.0.8_vue2\node_modules\vue-cli-plugin-electron-builder\index.js:198
? ? ? ? ? ? ? throw new Error('Build failed with errors.')
? ? ? ? ? ? ? ? ? ? ^Error: Build failed with errors.
? ? at C:\Users\63002\Desktop\elctron29.0.0_node18.19.0_vuecli5.0.8_vue2\node_modules\vue-cli-plugin-electron-builder\index.js:198:21
? ? at finalCallback (C:\Users\63002\Desktop\elctron29.0.0_node18.19.0_vuecli5.0.8_vue2\node_modules\vue-cli-plugin-electron-builder\node_modules\webpack\lib\Compiler.js:257:39)
? ? at C:\Users\63002\Desktop\elctron29.0.0_node18.19.0_vuecli5.0.8_vue2\node_modules\vue-cli-plugin-electron-builder\node_modules\webpack\lib\Compiler.js:273:13
? ? at AsyncSeriesHook.eval [as callAsync] (eval at create (C:\Users\63002\Desktop\elctron29.0.0_node18.19.0_vuecli5.0.8_vue2\node_modules\vue-cli-plugin-electron-builder\node_modules\tapable\lib\HookCodeFactory.js:33:10), <anonymous>:15:1)
? ? at AsyncSeriesHook.lazyCompileHook (C:\Users\63002\Desktop\elctron29.0.0_node18.19.0_vuecli5.0.8_vue2\node_modules\vue-cli-plugin-electron-builder\node_modules\tapable\lib\Hook.js:154:20)
? ? at onCompiled (C:\Users\63002\Desktop\elctron29.0.0_node18.19.0_vuecli5.0.8_vue2\node_modules\vue-cli-plugin-electron-builder\node_modules\webpack\lib\Compiler.js:271:21)
? ? at C:\Users\63002\Desktop\elctron29.0.0_node18.19.0_vuecli5.0.8_vue2\node_modules\vue-cli-plugin-electron-builder\node_modules\webpack\lib\Compiler.js:681:15
? ? at AsyncSeriesHook.eval [as callAsync] (eval at create (C:\Users\63002\Desktop\elctron29.0.0_node18.19.0_vuecli5.0.8_vue2\node_modules\tapable\lib\HookCodeFactory.js:33:10), <anonymous>:6:1)
? ? at AsyncSeriesHook.lazyCompileHook (C:\Users\63002\Desktop\elctron29.0.0_node18.19.0_vuecli5.0.8_vue2\node_modules\vue-cli-plugin-electron-builder\node_modules\tapable\lib\Hook.js:154:20)
? ? at C:\Users\63002\Desktop\elctron29.0.0_node18.19.0_vuecli5.0.8_vue2\node_modules\vue-cli-plugin-electron-builder\node_modules\webpack\lib\Compiler.js:678:31
? ? at AsyncSeriesHook.eval [as callAsync] (eval at create (C:\Users\63002\Desktop\elctron29.0.0_node18.19.0_vuecli5.0.8_vue2\node_modules\tapable\lib\HookCodeFactory.js:33:10), <anonymous>:6:1)
? ? at AsyncSeriesHook.lazyCompileHook (C:\Users\63002\Desktop\elctron29.0.0_node18.19.0_vuecli5.0.8_vue2\node_modules\vue-cli-plugin-electron-builder\node_modules\tapable\lib\Hook.js:154:20)
? ? at C:\Users\63002\Desktop\elctron29.0.0_node18.19.0_vuecli5.0.8_vue2\node_modules\vue-cli-plugin-electron-builder\node_modules\webpack\lib\Compilation.js:1423:35
? ? at AsyncSeriesHook.eval [as callAsync] (eval at create (C:\Users\63002\Desktop\elctron29.0.0_node18.19.0_vuecli5.0.8_vue2\node_modules\vue-cli-plugin-electron-builder\node_modules\tapable\lib\HookCodeFactory.js:33:10), <anonymous>:6:1)
? ? at AsyncSeriesHook.lazyCompileHook (C:\Users\63002\Desktop\elctron29.0.0_node18.19.0_vuecli5.0.8_vue2\node_modules\vue-cli-plugin-electron-builder\node_modules\tapable\lib\Hook.js:154:20)
? ? at C:\Users\63002\Desktop\elctron29.0.0_node18.19.0_vuecli5.0.8_vue2\node_modules\vue-cli-plugin-electron-builder\node_modules\webpack\lib\Compilation.js:1414:32
? ? at eval (eval at create (C:\Users\63002\Desktop\elctron29.0.0_node18.19.0_vuecli5.0.8_vue2\node_modules\vue-cli-plugin-electron-builder\node_modules\tapable\lib\HookCodeFactory.js:33:10), <anonymous>:12:1)
? ? at C:\Users\63002\Desktop\elctron29.0.0_node18.19.0_vuecli5.0.8_vue2\node_modules\vue-cli-plugin-electron-builder\node_modules\webpack\node_modules\terser-webpack-plugin\dist\index.js:321:9
? ? at TaskRunner.run (C:\Users\63002\Desktop\elctron29.0.0_node18.19.0_vuecli5.0.8_vue2\node_modules\vue-cli-plugin-electron-builder\node_modules\webpack\node_modules\terser-webpack-plugin\dist\TaskRunner.js:48:7)
? ? at TerserPlugin.optimizeFn (C:\Users\63002\Desktop\elctron29.0.0_node18.19.0_vuecli5.0.8_vue2\node_modules\vue-cli-plugin-electron-builder\node_modules\webpack\node_modules\terser-webpack-plugin\dist\index.js:227:18)
? ? at _next0 (eval at create (C:\Users\63002\Desktop\elctron29.0.0_node18.19.0_vuecli5.0.8_vue2\node_modules\vue-cli-plugin-electron-builder\node_modules\tapable\lib\HookCodeFactory.js:33:10), <anonymous>:8:1)
? ? at eval (eval at create (C:\Users\63002\Desktop\elctron29.0.0_node18.19.0_vuecli5.0.8_vue2\node_modules\vue-cli-plugin-electron-builder\node_modules\tapable\lib\HookCodeFactory.js:33:10), <anonymous>:23:1)
? ? at processTicksAndRejections (node:internal/process/task_queues:95:5)Node.js v18.19.0
C:\Users\63002\Desktop\elctron29.0.0_node18.19.0_vuecli5.0.8_vue2>
解決方案
打開package.json,找到
"scripts": {"electron:build": "vue-cli-service electron:build",},
改為
"scripts": {"electron:build": "set NODE_OPTIONS=--openssl-legacy-provider && vue-cli-service electron:build",},
?通過設置 NODE_OPTIONS 環境變量為 --openssl-legacy-provider,可以強制使用舊版的 OpenSSL 提供程序,從而解決構建過程中的報錯問題。
再次打包測試
?
可以正常打包,不報錯了。?
注意:
1.打包windows桌面端軟件要在windows系統的真機或者虛擬機上進行打包。同樣打包Mac系統的桌面端軟件要在Mac系統的真機或者虛擬機上進行打包。其他操作系統同理。
2.如果要打包發布到對應應用商店需要代碼簽名。https://www.electronjs.org/zh/docs/latest/tutorial/%E6%89%93%E5%8C%85%E6%95%99%E7%A8%8B
7.安裝打包后的軟件
源碼:GitHub - 1t1824d/elctron29.0.0_node18.19.0_vuecli5.0.8_vue2?
?參考:
1.解決npm淘寶鏡像到期問題_npm淘寶鏡像過期了怎么辦-CSDN博客
2.npm---緩存_npm cache-CSDN博客?
3.nvm,nrm,npx的使用(淘寶源證書到期解決) - 知乎
4.vue_electron問題總結_vue add electron-builder electron 版本問題-CSDN博客
5.npm安裝Electron 項目失敗報錯問題和解決辦法_npm install electron報錯-CSDN博客
6.【Electron+Vue】Error: error:0308010C:digital envelope routines::unsupported_background.js from terser error: error:0308010c:di-CSDN博客
7.解決electron+vue項目起始加載慢的問題_electron嵌入的網頁加載太慢-CSDN博客?