Electron windows下安装与打包成 exe 可执行程序

一 、安装

Electron做为一种用javascript写桌面程序的开发方式,如今已经被大众接受。下面就介绍如何在windows(>win7)下快速搭建Electron开发环境。javascript

1. nodejs 的安装

http://nodejs.cn/download/
复制代码

从下载最新版本的windows安装程序进行安装,我下载的是v6.9.1,安装时一路默认便可,这个安装会把nodejs和npm配置到系统PATH中,这样在命令行的任何位置均可以直接用node执行nodejs,用npm执行npm命令。 检查nodejs是否安装成功能够这样查看:css

另外,由于可能的GFW问题(否则会下载很慢很慢,也可能下载失败),因此建议把npm的仓库切换到国内taobao仓库,执行下面的命令:html

npm install -g cnpm --registry=https://registry.npm.taobao.org
复制代码

运行效果以下:前端

2. Electron的安装

由于前面已经启用了node/npm,因此能够采用npm的方法安装Electron。 为了测试的方便,我是在命令行窗口中采用下面的命令:html5

npm install -g electron
复制代码

效果以下:java

其中,查看electron是否安装成功可经过命令 electron -v 查看。 网上有的说命令是 npm install -g electron 以下:node

3. 编程环境安装

强烈推荐微软退出的VS Code,直接下载安装便可,它支持nodejs等的代码提示,非常方便。git

4. 打包输出工具

为了方便最终成果输出,建议安装electron-packager工具,安装也很简单,建议如下面的命令全局安装:程序员

npm install -g electron-packager
复制代码

效果以下:github

5. 若是须要采用git进行版本控制,建议安装git工具

直接在git 下载最新版本的安装便可。

https://git-scm.com/downloads
复制代码

至此实际上开发环境已经搭建好了。

二 、初学Electron

快速开始 Electron经过为运行时提供丰富的本机(操做系统)API,可使用纯JavaScript建立桌面应用程序。您能够将其看做是Node.js运行时的一种变体,它专一于桌面应用程序而不是Web服务器。 这并不意味着Electron是javascript绑定到图形用户界面(GUI)库。相反,Electron使用网页做为其GUI,所以您也能够将其视为由JavaScript控制的最小的Chromium浏览器。 主要过程 在电子,在运行过程当中package.json的main脚本被称为主处理。在主进程中运行的脚本能够经过建立网页来显示GUI。 渲染器过程 因为Electron使用Chromium来显示网页,所以也使用了Chromium的多进程架构。Electron中的每一个网页都在其本身的进程中运行,这称为渲染器进程。 在正常的浏览器中,网页一般在沙箱环境中运行,而且不容许访问本机资源。然而,电子用户有能力在网页中使用node.js API,容许较低级别的操做系统交互。 主进程和渲染器进程之间的差别 主进程经过建立BrowserWindow实例来建立网页。每一个BrowserWindow实例在其本身的渲染器进程中运行网页。当BrowserWindow实例被销毁时,相应的渲染器进程也被终止。 主进程管理全部网页及其相应的渲染器进程。每一个渲染器进程是隔离的,只关心在其中运行的网页。 在网页中,不容许调用本地GUI相关的API,由于管理网页中的本地GUI资源是很是危险的,而且容易泄漏资源。若是要在Web页面中执行GUI操做,则Web页面的渲染器进程必须与主进程通讯,以 请求主进程执行这些操做。 在Electron中,咱们有几种方法在主进程和渲染器进程之间进行通讯。Like ipcRenderer和ipcMain用于发送消息的模块,以及用于RPC样式通讯的远程模块。还有一个关于如何在网页之间共享数据的常见问题条目。

3、写你的第一电子应用程序

一般,Electron应用程序的结构以下:

your-app/

    ├── package.json

    ├── main.js

    └── index.html
复制代码

其格式与package.jsonNode模块的格式彻底相同,main字段指定的脚本是应用程序的启动脚本,它将运行主进程。您的示例package.json可能以下所示:

{
  "name"    : "your-app",
  "version" : "0.1.0",
  "main"    : "main.js"
}
复制代码

注意:若是main字段不存在package.json,Electron将尝试加载index.js。 本main.js应建立窗口和处理系统事件,一个典型的例子是:

const {app, BrowserWindow} = require('electron')
const path = require('path')
const url = require('url')

// 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(url.format({ pathname: path.join(__dirname, 'index.html'), protocol: 'file:', slashes: true })) // 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 macOS 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 macOS 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. 复制代码

最后index.html是您要显示的网页:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>Hello World!</title>
  </head>
  <body>
    <h1>Hello World!</h1>
    We are using node <script>document.write(process.versions.node)</script>,
    Chrome <script>document.write(process.versions.chrome)</script>,
    and Electron <script>document.write(process.versions.electron)</script>.
  </body>
</html>
复制代码

4、运行您的应用程序

一旦你建立了初始main.js,index.html和package.json文件,你可能想尝试在本地运行你的应用程序来测试它,并确保它的工做正常。 electron electron 是 npm 包含Electron的预编译版本的模块。 若是您已经在全局安装npm,那么您只须要在应用程序的源目录中运行如下命令:

electron .   //到当前目录下

$ .\node_modules\.bin\electron . 
复制代码

5、手动下载的电子二进制

做为分发版运行 完成编写应用程序后,您能够按照应用程序分发指南建立分发,而后再执行打包的应用程序。 试试这个例子 使用存储electron/electron-quick-start库克隆并运行本教程中的代码。 注意:运行此操做须要在系统上使用Git和Node.js(其中包括npm)。

//下载Nodejs 模块
# Clone the repository
$ git clone https://github.com/electron/electron-quick-start
# Go into the repository
$ cd electron-quick-start
# Install dependencies
$ npm install
# Run the app
$ npm start
复制代码

6、打包

在 HTML5的崛起、JavaScript要一统天下之际,有一个名为【跨平台】的技术愈来愈火。为何会这么火?由于软件开发者只需一次编写程序,便可在 Windows、Linux、Mac、iOS、Android 等平台运行,大大下降了程序员的工做量,也使公司的产品能够快读迭代。曾经跨平台技术的不被看好,现在随着手机、电脑硬件的发展而快速发展。这一切,几乎由html5技术推进,固然,javascript 这个语言,是最大的功臣。 基于 Html5 的跨平台技术比较出名的有 PhoneGap、Cordova,经常用于开发 webapp;还有 Egret、cocos-creator、Unity 等,经常使用于开发游戏;还有基于 Node.js 的 nw.js,用于开发桌面应用,以及 Electron,一款比 nw.js 还强大的用网页技术来开发桌面应用的神器。 其实,以上都是废话,如今进入主题:怎么用 Electron 将网页打包成 exe 可执行文件!

假设:

一、你已经安装并配置好了 node.js (全局安装)

二、你已经用 npm 安装了 electron (全局安装)

三、你已经写好了前端网页(html、css、javascript 这些,或者基于这些的前端框架写好的网页)

四、以上三点看不懂的,赶忙去百度。。。

你若是具有了以上的假设,请继续往下看:

一、找到你的前端网页项目文件夹,新建 package.json、main.js、index.html 三个文件(注:其中的 index.html 是你的网页首页)

你的项目目录/

├── package.json
├── main.js
└── index.html
复制代码

二、在 package.json 中添加以下内容

{ "name" : "app-name", "version" : "0.1.0", "main" : "main.js" }

三、在 main.js 中添加下面的内容,这个 main.js 文件就是上面 package.json 中的 "main"键 的值,因此可根据须要修改

const {app, BrowserWindow} = require('electron')
const path = require('path')
const url = require('url')

// 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(url.format({ pathname: path.join(__dirname, 'index.html'), protocol: 'file:', slashes: true })) // 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 macOS 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 macOS 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. 复制代码

四、若是你的网页首页的文件名不是 “index.html”,那么请在 main.js 中将其中的 'index.html' 修改成你的网页首页名

五、打开 DOS,cd 到你的项目目录(或直接在你的项目目录下空白的地方 shift+鼠标右键,而后点击在此处打开命令窗口,这里看不懂的,唉,百度吧少年)

六、在上一步的 DOS 下,输入 npm install electron-packager -g全局安装咱们的打包神器

npm install electron-packager -g
复制代码

七、安装好打包神器后,仍是在上一步的 DOS 下,输入 electron-packager . app --win --out presenterTool --arch=x64 --version 1.4.14 --overwrite --ignore=node_modules 便可开始打包

electron-packager . app --win --out presenterTool --arch=x64
 --version 1.4.14 --overwrite --ignore=node_modules
复制代码

这个命令什么意思?汉字是可自行修改:

electron-packager . 可执行文件的文件名 --win --out 打包成的文件夹名 --arch=x64位仍是32位 --version版本号 --overwrite --ignore=node_modules
复制代码

八、打包成功后,会生成一个新的文件夹,点进去,找到 exe 文件,双击就能够看到网页变成了一个桌面应用啦!

以上是最简单的打包方式,至于怎么修改窗口大小、菜单栏怎么加、怎么调用系统API这些,就给你慢慢去研究Electron了。

成功打包,惋惜,整个文件夹有130Mb那么大,其实不方便分发。

相关文章
相关标签/搜索