使用node-webkit创建桌面应用程序

在本文中,我将通过在Mac和Windows上为Web应用程序部署和构建程序包来介绍node-webkit的内容。

警告:此帖子过时,可能无法反映当前的最新状态

首先,免责声明:我不会讨论跑步Node.js代码,但仅打包要在Mac和Windows上运行的Web应用程序。 Linux也是游戏的一部分,但我没有介绍它。

节点Webkit就是这样,它是从其创建者Web运行时调用的。

它基于Chromium(尽管名称)和node.js。它使我们可以直接从DOM调用node.js代码和模块,并为使用Web技术编写的本机应用程序打开了新的可能性。

在本文中,我只是通过在Mac和Windows上为Web应用程序部署和构建程序包来对其进行介绍,就像它是本机应用程序一样。

运行应用

首先,让我们介绍index.html文件

<html>
  <body>
    <p>test</p>
  </body>
</html>

We also need a package.json file

{
  "name": "My Web App",
  "main": "index.html"
}

That’s it from the code-side! Really.

Now to run the app, download the runtime from the site https://github.com/rogerwang/node-webkit for your specific platform, and then you can call

1)
$ alias nw="open -n -a node-webkit '/PATH_TO_APP_DIRECTORY/'
$ ./nw

cd /PATH_TO_APP_DIRECTORY/ zip -r app.nw ./

You have now created a package of the app. You can double-click it on Mac, or drag over the node-webkit application on Windows, and it will run.

There are a lot of options you can now add to the package.json file https://github.com/rogerwang/node-webkit/wiki/Manifest-format, but that’s a start. You can for example hide the top bar with address and debugger, set the menu items, and decide pretty much all you need.

Now, let’s dig into packaging and distributing an app.

Package the app on the Mac

Unzip the node-webkit.app package downloaded from the node-webkit site.

Right-click, “Show package content”, go in Contents/Resources and copy there the app.nw file built with

$ zip -r app.nw ./

or alternatively, copy the app directory there and name it “app.nw”.

That’s it! You can now change the default icon and properties in the Contents/Info.plist file.

Package the app on Windows

Download the latest Windows package.

Copy the .nw file built using Zip in the package directory, alongside nw.exe, and call it app.nw

Run the command

copy /b nw.exe+app.nw app.exe

You can now remove the nw.exe & app.nw files, zip the directory and distribute the package. Your users will need to download the zip file, uncompress and then run the app.exe file contained.


More devtools tutorials: