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

Ais*_*war 113 javascript windows exe node.js

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

node.exe app.js inputArg

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

App.exe inputArg

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

App.bat inputArg

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

Cla*_*ick 50

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

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

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

  1. 使用package.json在根目录中压缩所有文件
  2. 将扩展名从.zip更改为.nw
  3. 复制/ b nw.exe + app.nw app.exe

就像一个补充说明 - 我已经使用它发运了几个生产盒/安装CD应用程序,并且它运行良好.相同的应用程序在Windows,Mac,Linux和Web上运行.

  • 刚刚尝试了一下,效果很好!它得到了英特尔人员的支持。 (2认同)
  • 不确定这在 2019 年是否仍然是一个好方法。听起来它会捆绑在大型 WebKit 库中,这对于 HTML GUI 应用程序来说很好,但对于控制台应用程序来说则不必要。 (2认同)

Ric*_*asi 31

有一些替代品,包括免费和商业.我没有使用它们,但理论上它们应该工作:

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

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

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

  • 我尝试了"高级"Batch To EXE转换器,它就像一个魅力.您可以将依赖项打包到EXE文件中.谢谢您的帮助. (2认同)

sok*_*kie 28

对于任何绊倒这个问题的人来说,现在有两个项目可以从你的节点项目中创建exes,EncloseJSElectron.atom.io,它们略有不同:

  1. EncloseJS会将您的项目编译为本机代码,它们还包括资产和nodejs安装(系统不需要安装nodejs来执行.exe).您的源代码不会包含在内.生成的可执行文件是Windows ONLY(.exe).现在支持所有平台.它现在需要商业产品的许可证.
  2. Electron.atom.io虽然它不会压缩并为您生成.exe,但它可用于分发nodejs应用程序,因为它们提供了一种分发应用程序的方法.好处是电子是多平台的(windows,mac osx,linux),缺点是你的源代码将被包含在内,甚至很难提供一种在asar存档中分发你的应用程序的方法.它不会是防弹的,但它仍然比你的来源更好.

  • 请更新:EncloseJS现在支持Linux和OSX。 (2认同)
  • EncloseJS不再维护。继承人似乎是zeit / pkg(https://www.npmjs.com/package/pkg) (2认同)

jos*_*736 21

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

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

当然,如果像我一样 - 你.js用编辑器关联文件,以便双击它们打开你最喜欢的文本编辑器,这个建议将没有多大帮助.您还可以.js文件添加 "Execute with Node" 的右键单击菜单项,尽管此替代方案无法解决您的命令行需求.


最简单的解决方案可能就是使用批处理文件 - 您不必在脚本所在的文件夹中包含Node的副本.只需完全引用Node可执行文件:

"C:\Program Files (x86)\nodejs\node.exe" app.js %*
Run Code Online (Sandbox Code Playgroud)

另一种选择是这个非常简单的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);
    }
}
Run Code Online (Sandbox Code Playgroud)

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

由于这可能是相对广泛的兴趣,我继续在GitHub上提供这个,包括编译的exe如果与陌生人进入货车是你的事情.


小智 18

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

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

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

pkg exampleApp.js

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

  • 仓库名称现已更新为 [vercel/pkg](https://github.com/vercel/pkg) (2认同)

bit*_*fed 12

由于这个问题已得到解答,因此推出了另一种解决方案.

https://github.com/appjs/appjs

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

编辑:AppJS不再处于活动状态,但本身建议使用名为deskshell的fork.

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

  • 似乎链接已损坏。 (2认同)
  • 该项目不再活跃。请参阅 Clayton Gulick 提到的替代方案 node-webkit。 (2认同)
  • *这是打包node.js的最终解决方案。...不再有效*大声笑 (2认同)

B T*_*B T 12

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

https://github.com/crcn/nexe


Nep*_*oxx 11

这里有很多好的答案,但它们并不像JXcore那么简单.

一旦在Windows上安装了JXcore,您只需运行:

jx package app.js "myAppName" -native
Run Code Online (Sandbox Code Playgroud)

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

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

编辑2018

该项目现在已经死了但它仍然在这里托管:https://github.com/jxcore/jxcore-release(感谢@Elmue)

  • 谢了哥们。我可以从 Windows 制作 Linux 可执行文件吗? (2认同)
  • 据我所知,不,你不能。我认为 JXCore 从操作系统中获取一些文件来制作可执行文件,并且它不会打包这些文件,因此如果您想制作 Linux 可执行文件,则必须在 Linux 上运行此命令。 (2认同)
  • JXcore已停止开发http://www.nubisa.com/nubisa-halting-active-development-on-jxcore-platform/ (2认同)
  • 这里所有的链接都失效了。这有效:https://github.com/jxcore/jxcore-release (2认同)

Har*_*ono 11

我正在使用以下技术:

  1. @vercel/ncc(这确保我们将所有必要的依赖项捆绑到单个文件中)
  2. pkg(用于制作exe文件)

让我们做如下:

  1. npm i -g @vercel/ncc

  2. ncc build app.ts -o dist (我的入口文件是app.ts,输出在dist文件夹中,确保你在package.json和app.ts所在的文件夹中运行,上面运行后你可能会看到index.js文件在文件夹 dist 中)

  3. npm install -g pkg(安装pkg)

  4. pkg index.js(确保您位于上面的 dist 文件夹中)


Min*_*Pan 7

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

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

  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

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

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

It is better than disclose in 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.

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

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仅支持具有支持SquashFS内核的Linux,而Node.js编译器支持Linux,macOS和Windows的所有三个平台,同时对该内核没有任何特殊功能的要求。


Dhr*_*ubo 5

nexe npm 包从您的 Node.js 应用程序中创建一个可执行文件

下一个 git 仓库


cib*_*en1 5

目前,我认为要走的路是deno

这是

deno compile mainProgram.js
Run Code Online (Sandbox Code Playgroud)

现有的节点项目可能需要某种转换,至少对于 require 语句(将节点项目转换为 deno)


归档时间:

查看次数:

122356 次

最近记录:

6 年,4 月 前