Javascript 如何从 node.js 应用程序制作 exe 文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8173232/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 04:57:41  来源:igfitidea点击:

How to make exe files from a node.js app?

javascriptwindowsnode.jsexe

提问by Aishwar

I have a node app that I wrote, that I run as follows:

我有一个我编写的节点应用程序,我按如下方式运行:

node.exe app.js inputArg

node.exe app.js inputArg

Is there some way I can package this into a .exe by itself? So I can just do something like this?

有什么方法可以将它自己打包成 .exe 吗?所以我可以做这样的事情吗?

App.exe inputArg

App.exe inputArg

I have some way of faking this by using a batch file, so I can do this:

我有一些使用批处理文件来伪造它的方法,所以我可以这样做:

App.bat inputArg

App.bat inputArg

But this requires that I have all the dependencies and node in that folder, which is not ideal.

但这要求我在该文件夹中拥有所有依赖项和节点,这并不理想。

采纳答案by Ricardo Tomasi

There a few alternatives, both free and commercial. I haven't used any of them but in theory they should work:

有一些替代方案,包括免费的和商业的。我没有使用过它们中的任何一个,但理论上它们应该可以工作:

Most will require you to keep the batch file as main executable, and then bundle node.exe and your scripts.

大多数将要求您将批处理文件保留为主可执行文件,然后捆绑 node.exe 和您的脚本。

Depending on your script, you also have the option to port it to JSDB, which supports an easy way to create executablesby simply appending resources to it.

根据您的脚本,您还可以选择将其移植到JSDB,它支持通过简单地向其附加资源来创建可执行文件的简单方法

A third quasi-solution is to keep node somewhere like C:\utilsand add this folder to your PATHenvironment variable. Then you can create .bat files in that dir that run node + your preferred scripts - I got coffeescript's coffeeworking on windows this way. This setup can be automated with a batch file, vb script or installer.

第三个准解决方案是将节点保留在某个地方C:\utils并将此文件夹添加到您的PATH环境变量中。然后你可以在那个运行节点的目录中创建 .bat 文件+你喜欢的脚本——我让coffeescriptcoffee在windows上以这种方式工作。可以使用批处理文件、vb 脚本或安装程序自动执行此设置。

回答by Clayton Gulick

The solution I've used is Roger Wang's node-webkit.

我使用的解决方案是 Roger Wang 的node-webkit

This is a fantastic way to package nodejs apps and distribute them, it even gives you the option to "bundle" the whole app as a single executable. It supports windows, mac and linux.

这是打包和分发 nodejs 应用程序的绝佳方式,它甚至让您可以选择将整个应用程序“捆绑”为单个可执行文件。它支持windows、mac和linux。

Hereare some docs on the various options for deploying node-webkit apps, but in a nutshell, you do the following:

以下是有关部署 node-webkit 应用程序的各种选项的一些文档,但简而言之,您可以执行以下操作:

  1. Zip up all your files, with a package.json in the root
  2. Change the extension from .zip to .nw
  3. copy /b nw.exe+app.nw app.exe
  1. 压缩所有文件,在根目录中包含 package.json
  2. 将扩展名从 .zip 更改为 .nw
  3. 复制 /b nw.exe+app.nw app.exe

Just as an added note - I've shipped several production box/install cd applications using this, and it's worked great. Same app runs on windows, mac, linux and over the web.

就像附加说明一样 - 我已经使用它运送了几个生产盒/安装 cd 应用程序,并且它运行良好。同一个应用程序可以在 windows、mac、linux 和网络上运行。

Update: the project name has changed to 'nw.js' and is properly located here: nw.js

更新:项目名称已更改为“nw.js”并正确位于此处:nw.js

回答by sokie

For anyone stumbling upon this question, there are now two projects that create exes out of your node projects, Pkgand Electron.atom.io, they differ slightly:

对于任何遇到这个问题的人,现在有两个项目可以从您的节点项目中创建 exe,PkgElectron.atom.io,它们略有不同:

  1. Pkgwill compile your project to native code, they also include assets AND a nodejs installation ( the system won't need to install nodejs to execute the .exe ). Your source code will not be included. The resulting executable is Windows ONLY ( .exe ).All platforms are supported now. It now requires a licence for commercial products.Fully open source.
  2. Electron.atom.iowhile it will not compact and generate an .exe for you, it CAN be used to distribute nodejs apps since they offer a way to distribute applications. The benefits are that electron is multi-platform ( windows, mac osx, linux ), the cons are that your source code WILL be included, even tough they offer a way to distribute your app inside an asararchive. It will not be bulletproof but it's still better than your source in plain.
  1. Pkg会将您的项目编译为本机代码,它们还包括资产和 nodejs 安装(系统不需要安装 nodejs 来执行 .exe )。您的源代码将不包括在内。生成的可执行文件仅适用于 Windows ( .exe )。现在支持所有平台。它现在需要商业产品的许可证。完全开源。
  2. Electron.atom.io虽然它不会压缩并为您生成 .exe,但它可以用于分发 nodejs 应用程序,因为它们提供了一种分发应用程序的方法。好处是电子是多平台的(windows、mac osx、linux),缺点是你的源代码将被包含在内,即使它们提供了一种在asar存档中分发你的应用程序的方法。它不会是防弹的,但它仍然比你的源代码更好。

回答by josh3736

By default, Windows associates .jsfiles with the Windows Script Host, Microsoft's stand-alone JS runtime engine. If you type script.jsat a command prompt (or double-click a .jsfile in Explorer), the script is executed by wscript.exe.

默认情况下,Windows 将.js文件与Windows Script Host(Microsoft 的独立 JS 运行时引擎)相关联。如果您在命令提示符下键入script.js(或.js在资源管理器中双击文件),则脚本由wscript.exe.

This may be solving a local problem with a global setting, but you could associate.jsfiles with node.exeinstead, so that typing script.jsat a command prompt or double-clicking/dragging items onto scripts will launch them with Node.

这可能是通过全局设置解决本地问题,但您可以将文件与之关联,以便在命令提示符下键入script.js或双击/拖动项目到脚本上,将使用 Node 启动它们。.jsnode.exe

Of course, if—like me—you've associated .jsfiles with an editor so that double-clicking them opens up your favorite text editor, this suggestion won't do much good. You could also add a right-click menu entryof "Execute with Node" to .jsfiles, although this alternative doesn't solve your command-line needs.

当然,如果 - 像我一样 - 你已经将.js文件与编辑器相关联,以便双击它们打开你最喜欢的文本编辑器,这个建议不会有多大用处。您还可以.js文件添加“使用节点执行”的右键单击菜单项,尽管此替代方法不能解决您的命令行需求。



The simplest solution is probably to just use a batch file – you don't have to have a copy of Node in the folder your script resides in. Just reference the Node executable absolutely:

最简单的解决方案可能是只使用批处理文件——您不必在脚本所在的文件夹中拥有 Node 的副本。只需绝对引用 Node 可执行文件:

"C:\Program Files (x86)\nodejs\node.exe" app.js %*


Another alternative is this very simple C# app which will start Node using its own filename + .jsas the script to run, and pass along any command line arguments.

另一种选择是这个非常简单的 C# 应用程序,它将使用自己的文件名 +.js作为要运行的脚本启动 Node ,并传递任何命令行参数。

class Program
{
    static void Main(string[] args)
    {
        var info = System.Diagnostics.Process.GetCurrentProcess();
        var proc = new System.Diagnostics.ProcessStartInfo(@"C:\Program Files (x86)\nodejs\node.exe", "\"" + info.ProcessName + ".js\" " + String.Join(" ", args));
        proc.UseShellExecute = false;
        System.Diagnostics.Process.Start(proc);
    }
}

So if you name the resulting EXE "app.exe", you can type app arg1 ...and Node will be started with the command line "app.js" arg1 .... Note the C# bootstrapper app will immediately exit, leaving Node in charge of the console window.

因此,如果您将生成的 EXE 命名为“app.exe”,您可以键入app arg1 ...,Node 将使用命令行启动"app.js" arg1 ...。请注意,C# 引导程序应用程序将立即退出,让 Node 负责控制台窗口。

Since this is probably of relatively wide interest, I went ahead and made this available on GitHub, including the compiled exeif getting in to vans with strangers is your thing.

由于这可能是相对广泛的兴趣,我继续在 GitHub 上提供了它,包括编译的 exe,如果你喜欢和陌生人一起上货车的话。

回答by user8376713

The best tool I know is NodeJS tool: zeit/pkg

我所知道的最好的工具是 NodeJS 工具:zeit/pkg

It is very easy to use (much more than Nexe, just as an example), you can just install in globally: npm install -g pkg

它非常易于使用(比 Nexe 多得多,仅作为示例),您只需全局安装即可: npm install -g pkg

to create executables for macOS, Linux and Windows:

为 macOS、Linux 和 Windows 创建可执行文件:

pkg exampleApp.js

pkg exampleApp.js

I had a bit of complicated code which used NodeJS socket server, I tried different applications, none of them created it properly, except zeit/pkg.

我有一些使用 NodeJS 套接字服务器的复杂代码,我尝试了不同的应用程序,除了zeit/pkg之外,没有一个应用程序正确创建了它。

回答by B T

Haven't tried it, but nexe looks like nexe can do this:

没试过,但 nexe 看起来像 nexe 可以做到这一点:

https://github.com/crcn/nexe

https://github.com/crcn/nexe

回答by bitfed

Since this question has been answered, another solution has been launched.

既然已经回答了这个问题,又推出了另一种解决方案。

https://github.com/appjs/appjs

https://github.com/appjs/appjs

At the time of this writing, this is the end-all solution for packaging node.js apps through a stripped down chromium package compiled into an executable.

在撰写本文时,这是通过编译为可执行文件的精简铬包打包 node.js 应用程序的最终解决方案。

Edit: AppJS is no longer active, but itself suggests a fork called deskshell.

编辑:AppJS 不再处于活动状态,但它本身暗示了一个名为deskshell 的叉子。

https://github.com/sihorton/appjs-deskshell/

https://github.com/sihorton/appjs-deskshell/

回答by Nepoxx

There are a lot of good answers here, but they're not all as straightforward as JXcore.

这里有很多很好的答案,但它们并不像JXcore那样直接。

Once you have JXcore installed on windows, all you have to do is run:

在 Windows 上安装 JXcore 后,您所要做的就是运行:

jx package app.js "myAppName" -native

This will produce a .exe file that you can distribute and can be executed without any external dependencies whatsoever (you don't even need JXcore nor Node.js on the system).

这将生成一个 .exe 文件,您可以分发该文件并且可以在没有任何外部依赖的情况下执行该文件(您甚至不需要系统上的 JXcore 或 Node.js)。

Here's the documentation on that functionality: http://jxcore.com/packaging-code-protection/#cat-74

这是有关该功能的文档:http: //jxcore.com/packaging-code-protection/#cat-74

Edit 2018

编辑 2018

That project is now dead but it is still hosted here: https://github.com/jxcore/jxcore-release(thanks @Elmue)

该项目现已死亡,但仍托管在此处:https: //github.com/jxcore/jxcore-release(感谢@Elmue)

回答by Minqi Pan

Try disclose: https://github.com/pmq20/disclose

试试disclosehttps: //github.com/pmq20/disclose

discloseessentially makes a self-extracting exe out of your Node.js project and Node.js interpreter with the following characteristics,

disclose本质上是从你的 Node.js 项目和 Node.js 解释器中制作一个自解压的 exe,具有以下特征,

  1. No GUI. Pure CLI.
  2. No run-time dependencies
  3. Supports both Windows and Unix
  4. Runs slowly for the first time (extracting to a cache dir), then fast forever
  1. 没有图形用户界面。纯 CLI。
  2. 没有运行时依赖
  3. 支持 Windows 和 Unix
  4. 第一次运行缓慢(提取到缓存目录),然后永远快速

Try node-compiler: https://github.com/pmq20/node-compiler

试试node-compilerhttps: //github.com/pmq20/node-compiler

I have made a new project called node-compilerto compile your Node.js project into one single executable.

我创建了一个名为node-compiler将 Node.js 项目编译为一个可执行文件的新项目。

It is better than disclosein that it never runs slowly for the first time, since your source code is compiled together with Node.js interpreter, just like the standard Node.js libraries.

它比disclose第一次运行时从不缓慢要好,因为您的源代码是与 Node.js 解释器一起编译的,就像标准的 Node.js 库一样。

Additionally, it redirect file and directory requests transparently to the memory instead of to the file system at runtime. So that no source code is required to run the compiled product.

此外,它在运行时将文件和目录请求透明地重定向到内存而不是文件系统。这样就不需要源代码来运行编译后的产品。

How it works: https://speakerdeck.com/pmq20/node-dot-js-compiler-compiling-your-node-dot-js-application-into-a-single-executable

工作原理:https: //speakerdeck.com/pmq20/node-dot-js-compiler-compiling-your-node-dot-js-application-into-a-single-executable

Comparing with Similar Projects,

与同类项目相比,

  • pkg(https://github.com/zeit/pkg): Pkg hacked fs.* API's dynamically in order to access in-package files, whereas Node.js Compiler leaves them alone and instead works on a deeper level via libsquash. Pkg uses JSON to store in-package files while Node.js Compiler uses the more sophisticated and widely used SquashFS as its data structure.

  • EncloseJS(http://enclosejs.com/): EncloseJS restricts access to in-package files to only five fs.* API's, whereas Node.js Compiler supports all fs.* API's. EncloseJS is proprietary licensed and charges money when used while Node.js Compiler is MIT-licensed and users are both free to use it and free to modify it.

  • Nexe(https://github.com/nexe/nexe): Nexe does not support dynamic require because of its use of browserify, whereas Node.js Compiler supports all kinds of require including require.resolve.

  • asar(https://github.com/electron/asar): Asar uses JSON to store files' information while Node.js Compiler uses SquashFS. Asar keeps the code archive and the executable separate while Node.js Compiler links all JavaScript source code together with the Node.js virtual machine and generates a single executable as the final product.

  • AppImage(http://appimage.org/): AppImage supports only Linux with a kernel that supports SquashFS, while Node.js Compiler supports all three platforms of Linux, macOS and Windows, meanwhile without any special feature requirements from the kernel.

  • pkg( https://github.com/zeit/pkg):Pkg 动态地破解了 fs.* API 以访问包内文件,而 Node.js Compiler 不理会它们,而是通过 libsquash 在更深层次上工作。Pkg 使用 JSON 来存储包内文件,而 Node.js Compiler 使用更复杂和广泛使用的 SquashFS 作为其数据结构。

  • EncloseJS( http://enclosejs.com/):EncloseJS 将对包内文件的访问限制为仅五个 fs.* API,而 Node.js 编译器支持所有 fs.* API。EncloseJS 是专有许可的,使用时收费,而 Node.js 编译器是 MIT 许可的,用户可以自由使用它,也可以自由修改它。

  • Nexe( https://github.com/nexe/nexe):Nexe 不支持动态 require,因为它使用了 browserify,而 Node.js Compiler 支持包括 require.resolve 在内的所有类型的 require。

  • asar( https://github.com/electron/asar):Asar 使用 JSON 来存储文件信息,而 Node.js Compiler 使用 SquashFS。Asar 将代码存档和可执行文件分开,而 Node.js Compiler 将所有 JavaScript 源代码与 Node.js 虚拟机链接在一起,并生成单个可执行文件作为最终产品。

  • AppImage( http://appimage.org/):AppImage 仅支持内核支持 SquashFS 的 Linux,而 Node.js Compiler 支持 Linux、macOS 和 Windows 三个平台,同时对内核没有任何特殊功能要求。

回答by Aminadav Glickshtein

I'm recommending you BoxedApp for that. It is a commercial product that working very well. It works for any NodeJS app. The end-user will get just one EXE file for your app. No need for installation.

我为此向您推荐 BoxedApp。它是一种运行良好的商业产品。它适用于任何 NodeJS 应用程序。最终用户将只获得一个适用于您的应用程序的 EXE 文件。无需安装。

In Electron, for example, the end user needs to install/uncompress your app, and he will see all the source files of your code.

例如,在 Electron 中,最终用户需要安装/解压缩您的应用程序,他将看到您代码的所有源文件。

BoxedApp It is packaging all the files and dependencies that your NodeJS app needs. It supports compressions, and the compiled file works on Windows XP+

BoxedApp 它打包了您的 NodeJS 应用程序所需的所有文件和依赖项。它支持压缩,编译后的文件适用于 Windows XP+

When you use it, be sure to add Node.EXE to the compiled app. Choose a node version that supports Windows XP (If you need this)

使用的时候一定要在编译好的app中添加Node.EXE。选择支持 Windows XP 的节点版本(如果需要)

The program supports command line arguments, So after package, you can do

程序支持命令行参数,所以打包后,你可以做

c:\myApp argument1

And you can get the argument in your node code:

您可以在节点代码中获取参数:

process.argv[1]

Sometimes your app has files dependencies, so in BoxedApp you can add any folder and file as a dependency, for example, you can insert a settings file.

有时你的应用程序有文件依赖,所以在 BoxedApp 中你可以添加任何文件夹和文件作为依赖,例如,你可以插入一个设置文件。

var mySettings=require('fs').readFileSync('./settings.jgon').toString()

More info:

更多信息:

Just a note:usually I'm recommending about open-source/free software, but in this case I didn't found anything that gets the job done.

请注意:通常我会推荐开源/免费软件,但在这种情况下,我没有找到任何可以完成工作的东西。