首页
Javascript
Html
Css
Node.js
Electron
移动开发
工具类
服务端
浏览器相关
前端收藏
其他
关于

window下electron打包成exe

2018年12月03日 发布 阅读(1061) 作者:懒炖

官网搜索找到一个node-innonpmjs上看popularity只有4%。这里用一个npmjs使用较多的 electron-packager

PS: 用完electron-packager后,发现electron-builder更好~

支持支行平台

  • Windows(32/64位)
  • OS X(也称为macOS)
  • Linux(x86 / x86_64)

安装

nodejs版本至少要大于4.0
electron-packager比较大,安装很容易失败,请看

  1. npm install electron-packager --save-dev

报错了

  1. D:\project\electron>npm install electron-packager --save-dev
  2. > electron@3.0.7 postinstall D:\project\electron\node_modules\electron
  3. > node install.js
  4. Downloading tmp-14536-1-SHASUMS256.txt-3.0.7
  5. Error: connect ETIMEDOUT 52.216.16.216:443
  6. D:\project\electron\node_modules\electron\install.js:49
  7. throw err
  8. ^
  9. Error: connect ETIMEDOUT 52.216.16.216:443
  10. at Object._errnoException (util.js:1022:11)
  11. at _exceptionWithHostPort (util.js:1044:20)
  12. at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1198:14)
  13. npm ERR! code ELIFECYCLE
  14. npm ERR! errno 1
  15. npm ERR! electron@3.0.7 postinstall: `node install.js`
  16. npm ERR! Exit status 1
  17. npm ERR!
  18. npm ERR! Failed at the electron@3.0.7 postinstall script.
  19. npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

原因使用了官方npm源,太慢

  1. D:\project\electron>nrm ls
  2. * npm ---- https://registry.npmjs.org/
  3. cnpm --- http://r.cnpmjs.org/
  4. taobao - https://registry.npm.taobao.org/
  5. nj ----- https://registry.nodejitsu.com/
  6. rednpm - http://registry.mirror.cqupt.edu.cn/
  7. npmMirror https://skimdb.npmjs.com/registry/
  8. edunpm - http://registry.enpmjs.org/

切换到最快的cnpm后,还是失败

  1. D:\project\electron>nrm test
  2. * npm ---- 736ms
  3. cnpm --- 159ms
  4. taobao - 342ms
  5. nj ----- Fetch Error
  6. rednpm - Fetch Error
  7. npmMirror 1152ms
  8. edunpm - Fetch Error
  9. D:\project\electron>nrm use cnpm
  10. Registry has been set to: http://r.cnpmjs.org/
  11. D:\project\electron>npm install electron-packager --save-dev
  12. npm ERR! code ECONNRESET
  13. npm ERR! errno ECONNRESET
  14. npm ERR! network request to https://cnpmjs.oss-ap-southeast-1.aliyuncs.com/electron-osx-sign/-/electron-osx-sign-0.4.11.tgz failed, reason: read ECONNRESET
  15. npm ERR! network This is a problem related to network connectivity.
  16. npm ERR! network In most cases you are behind a proxy or have bad network settings.
  17. npm ERR! network
  18. npm ERR! network If you are behind a proxy, please make sure that the
  19. npm ERR! network 'proxy' config is set properly. See: 'npm help config'

切换到taobao后,安装成功

  1. D:\project\electron>nrm use taobao
  2. Registry has been set to: https://registry.npm.taobao.org/
  3. D:\project\electron>npm install electron-packager --save-dev
  4. > electron@3.0.7 postinstall D:\project\electron\node_modules\electron
  5. > node install.js
  6. Downloading tmp-11764-0-SHASUMS256.txt-3.0.7
  7. [============================================>] 100.0% of 4.74 kB (4.74 kB/s)
  8. + electron-packager@12.2.0
  9. added 2 packages and updated 1 package in 9.78s

打包命令

  1. electron-packager <sourcedir> <appname> --platform=<platform> --arch=<arch> [optional flags...]
常用参数
参数说明
sourcedir项目路径
appname打包后的名称
—platform要构建应用的目标平台(Windows、Mac 还是 Linux),允许值:linux,win32,darwin,mas,all
—arch用x86 还是用 x64 还是两个架构都用
—all设置true时,—platform和—arch都会设置为all
—icon图标,使用本地路径
—out输出目录
—ignore正则表达式,指定忽略的文件,out设置的目录会默认忽略
—overwrite覆盖已经存在的输出目录

更多参数,参数API: https://github.com/electron-userland/electron-packager/blob/master/docs/api.md

设置package.json

前面安装时,使用了--save-dev,所以只能在package.json里设置后运行。如果在直接在命令行运行,请全局-g安装

  1. ...
  2. "scripts": {
  3. "start": "electron .",
  4. "packager":"electron-packager ./ ElectronHelloWorld --out ./dist --appVersion 1.0.0 --overwrite"
  5. },
  6. ...

控制台执行npm run packager,会在dist目录生成ElectronHelloWorld-win32-x64文件夹

打开ElectronHelloWorld.exe文件,OK,Hello World出来了

版权声明:本站文章除特别声明外,均采用署名-非商业性使用-禁止演绎 4.0 国际 许可协议,如需转载,请注明出处

评论

  •