Node.js Electron windows下安装与打包成 exe

如有侵犯,请来信oiken@qq.com


一 安装

转载:http://www.cnblogs.com/phpCHAIN/p/6347044.html

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

1. nodejs 的安装

从nodejs 下载最新版本的windows安装程序进行安装,我下载的是v6.9.1,安装时一路默认即可,这个安装会把nodejs和npm配置到系统PATH中,这样在命令行的任何位置都可以直接用node执行nodejs,用npm执行npm命令。

检查nodejs是否安装成功可以这样查看:

另外,因为可能的GFW问题(不然会下载很慢很慢,也可能下载失败),所以建议把npm的仓库切换到国内taobao仓库,执行下面的命令:

npm config set registry "https://registry.npm.taobao.org/"

还有一种方式,注册cnpm命令,如下(我采用的是第二种):

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

运行效果如下:

2. Electron的安装

因为前面已经启用了node/npm,所以可以采用npm的方法安装Electron。
为了测试的方便,我是在命令行窗口中采用下面的命令:

npm install -g electron

效果如下:

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

网上有的说命令是 npm install -g electron-prebuilt,其实这是早期的版本命令,现在已经更新为electron了,如下:

3. 编程环境安装

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

4. 打包输出工具

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

npm install -g electron-packager

效果如下:

5. 如果需要采用git进行版本控制,建议安装git工具

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

 

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


二 初学Electron

转载自:http://blog.csdn.net/new0801/article/details/60866244


接触了两周的electron,感觉还不错,以后pc端基本上可以用electron加壳写pc端应用了,可以用nodejs的模块,也可以用es6、7,还可以直接操作系统文件。基本上可以完成大多数pc应用了。

就是安装慢成狗了。。。。用镜也卡!

快速开始

Electron通过为运行时提供丰富的本机(操作系统)API,可以使用纯JavaScript创建桌面应用程序。您可以将其看作是Node.js运行时的一种变体,它专注于桌面应用程序而不是Web服务器。

这并不意味着Electron是javascript绑定到图形用户界面(GUI)库。相反,Electron使用网页作为其GUI,因此您也可以将其视为由JavaScript控制的最小的Chromium浏览器。

主要过程

在电子,在运行过程中package.jsonmain脚本被称为主处理。在主进程中运行的脚本可以通过创建网页来显示GUI。

渲染器过程

由于Electron使用Chromium来显示网页,因此也使用了Chromium的多进程架构。Electron中的每个网页都在其自己的进程中运行,这称为渲染器进程

在正常的浏览器中,网页通常在沙箱环境中运行,并且不允许访问本机资源。然而,电子用户有能力在网页中使用node.js API,允许较低级别的操作系统交互。

主进程和渲染器进程之间的差异

主进程通过创建BrowserWindow实例来创建网页。每个BrowserWindow实例在其自己的渲染器进程中运行网页。当BrowserWindow实例被销毁时,相应的渲染器进程也被终止。

主进程管理所有网页及其相应的渲染器进程。每个渲染器进程是隔离的,只关心在其中运行的网页。

在网页中,不允许调用本地GUI相关的API,因为管理网页中的本地GUI资源是非常危险的,并且容易泄漏资源。如果要在Web页面中执行GUI操作,则Web页面的渲染器进程必须与主进程通信,以请求主进程执行这些操作。

在Electron中,我们有几种方法在主进程和渲染器进程之间进行通信。Like ipcRendereripcMain用于发送消息的模块,以及用于RPC样式通信的远程模块。还有一个关于如何在网页之间共享数据的常见问题条目。

写你的第一电子应用程序

通常,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>

 

运行您的应用程序

一旦你创建了初始main.jsindex.htmlpackage.json文件,你可能想尝试在本地运行你的应用程序来测试它,并确保它的工作正常。

electron

electronnpm包含Electron的预编译版本的模块。

如果您已经在全局安装npm,那么您只需要在应用程序的源目录中运行以下命令:

electron .

如果您已在本地安装,请运行:

macOS / Linux
$ ./node_modules/.bin/electron .

 

视窗
$ .\node_modules\.bin\electron .

 

手动下载的电子二进制

如果您手动下载了电子邮件,您还可以使用包含的二进制文件直接执行您的应用程序。

视窗
$ .\electron\electron.exe your-app\

 

Linux
$ ./electron/electron your-app/

 

macOS
$ ./Electron.app/Contents/MacOS/Electron your-app/

Electron.app这里是Electron的发行包的一部分,你可以从这里下载。

作为分发版运行

完成编写应用程序后,您可以按照应用程序分发指南创建分发,然后再执行打包的应用程序。

试试这个例子

使用存储electron/electron-quick-start库克隆并运行本教程中的代码。

注意:运行此操作需要在系统上使用Git和Node.js(其中包括npm)。

# 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



三 打包


转载自:http://blog.csdn.net/a727911438/article/details/70834467


在 HTML5的崛起、JavaScript要一统天下之际,有一个名为【跨平台】的技术越来越火。为什么会这么火?因为软件开发者只需一次编写程序,即可在 Windows、Linux、Mac、iOSAndroid 等平台运行,大大降低了程序员的工作量,也使公司的产品可以快读迭代。曾经跨平台技术的不被看好,如今随着手机、电脑硬件的发展而快速发展。这一切,几乎由html5技术推动,当然,javascript 这个语言,是最大的功臣。


基于 Html5 的跨平台技术比较出名的有 PhoneGap、Cordova,常常用于开发 webapp;还有 Egret、cocos-creator、Unity 等,常用于开发游戏;还有基于 Node.js 的 nw.js,用于开发桌面应用,以及 Electron,一款比 nw.js 还强大的用网页技术来开发桌面应用的神器。


其实,以上都是废话,现在进入主题:怎么用 Electron 将网页打包成 exe 可执行文件!


假设:

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

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

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

4、以上三点看不懂的,赶紧去百度。。。


你如果具备了以上的假设,请继续往下看:


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

你的项目目录/
├── package.json
├── main.js
└── index.html

2、在 package.json 中添加如下内容

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

3、在 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.

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


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


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

npm install electron-packager -g


7、安装好打包神器后,还是在上一步的 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




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






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




如果你打包总是不成功,觉得很烦,同时对扩展功能没什么要求的话,


点击进入我的Coding代码仓库:https://coding.net/u/linhongbijkm/p/Electron-packager-build-project/git


里面有我已将内容为 hello,world 的 index.html 网页通过 Electron 框架打包为 windows 环境下的桌面应用。


现只需将你的网页前端项目复制到 /resources/app/project 目录下,双击 exe 文件即可以桌面应用的方式运行你的网页。



四 打包出错的解决

oiken原创的:

1,windows下去其它盘例如d盘,直接打 D: 就可以了,我一直在打cd d:

结果进不去,百度解决


2,按照上面的命令打包出错了:   unable to determine electron version.  Please specify an Electron version.

上网百度,没有效果,只好看英文

用 electron-package --help 查看,确实有一个属性是 --electron-version

我就改成了:

electron-packager . HelloElectron --win --out=release --arch=x64 --version=1.0.0 --electron-version=1.7.5 --overwrite


不能加上 --ignore=node_modules,这样对于package.json里面的dependencies 无效了


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




  • 2
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值