如何将 Node.js 应用程序部署为单个可执行文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14314038/
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-09-02 16:56:24  来源:igfitidea点击:

How do I deploy Node.js applications as a single executable file?

node.jsdeployment

提问by Golo Roden

Supposed I have written a Node.js application, and I now would like to distribute it. Of course, I want to make it easy for the user, hence I do not want him to install Node.js, run npm installand then manually type node app.js.

假设我已经编写了一个 Node.js 应用程序,现在我想分发它。当然,我想让用户更容易,因此我不希望他安装 Node.js,运行npm install然后手动键入node app.js.

What I'd prefer was a single executable file, e.g. an .exefile on Windows.

我更喜欢单个可执行文件,例如.exeWindows 上的文件。

How could I approach this?

我怎么能接近这个?

I am aware of this thread, anyway this is only about Windows. How could I achieve this in a platform-independent manner? Any ideas? Best practices? ...?

我知道这个线程,无论如何这只是关于 Windows。我怎样才能以独立于平台的方式实现这一目标?有任何想法吗?最佳实践?……?

The perfect solution was a "compiler" I can give a source folder to. The source folder contains the app itself in various .jsfiles, the node_modulesfolder and some metadata, such as the package.json. The output should be binaries for various platforms, such as Windows, OS X and Linux.

完美的解决方案是一个“编译器”,我可以给它一个源文件夹。源文件夹在各种.js文件中包含应用程序本身、文件node_modules夹和一些元数据,例如package.json. 输出应该是各种平台的二进制文件,例如 Windows、OS X 和 Linux。

Oh, and what's important: I do not want to make anychanges to the source code, so calls to requirewith relative paths should still work, even if this relative path is now inside the packaged app.

哦,重要的是:我不想对源代码进行任何更改,因此require使用相对路径调用应该仍然有效,即使此相对路径现在位于打包的应用程序中。

Any ideas?

有任何想法吗?

PS: I do not want the user to install Node.js independently, it should be included inside the executable as well.

PS:我不希望用户独立安装 Node.js,它也应该包含在可执行文件中。

采纳答案by Golo Roden

Meanwhile I have found the (for me) perfect solution: nexe, which creates a single executable from a Node.js application including all of its modules.

同时,我找到了(对我而言)完美的解决方案:nexe,它从 Node.js 应用程序(包括其所有模块)创建单个可执行文件。

It's the next best thing to an ideal solution.

这是理想解决方案的次佳选择。

回答by Frederic

First, we're talking about packaging a Node.js app for workshops, demos, etc. where it can be handy to have an app "just running" without the need for the end user to care about installation and dependencies.

首先,我们讨论的是为研讨会、演示等打包 Node.js 应用程序,在这种情况下,可以方便地让应用程序“运行”而无需最终用户关心安装和依赖项。

You can try the following setup:

您可以尝试以下设置:

  1. Get your apps source code
  2. npm installall dependencies (via package.json) to the localnode_modules directory. It is important to perform this step on each platform you want to support separately, in case of binary dependencies.
  3. Copy the Node.js binary – node.exeon Windows, (probably) /usr/local/bin/nodeon OS X/Linux to your project's root folder. On OS X/Linux you can find the location of the Node.js binary with which node.
  1. 获取您的应用源代码
  2. npm install所有依赖项(通过 package.json)到本地node_modules 目录。在二进制依赖项的情况下,在您想要单独支持的每个平台上执行此步骤很重要。
  3. 将 Node.js 二进制文件 – Windows 上的node.exe,(可能)OS X/Linux 上的/usr/local/bin/node复制到项目的根文件夹中。在 OS X/Linux 上,您可以使用 .js 文件找到 Node.js 二进制文件的位置which node

For Windows:
Create a self extracting archive, 7zip_extrasupports a way to execute a command right after extraction, see: http://www.msfn.org/board/topic/39048-how-to-make-a-7-zip-switchless-installer/.

对于 Windows:
创建自解压存档,7zip_extra支持在解压后立即执行命令的方法,请参阅:http: //www.msfn.org/board/topic/39048-how-to-make-a-7-zip -无开关安装程序/

For OS X/Linux:
You can use tools like makeselfor unzipsfx(I don't know if this is compiled with CHEAP_SFX_AUTORUN defined by default).

对于 OS X/Linux:
您可以使用makeselfunzipsfx 之类的工具(我不知道这是否是使用默认定义的 CHEAP_SFX_AUTORUN 编译的)。

These tools will extract the archive to a temporary directory, execute the given command (e.g. node app.js) and remove all files when finished.

这些工具会将存档解压缩到一个临时目录,执行给定的命令(例如node app.js)并在完成后删除所有文件。

回答by Jason Nichols

Not to beat a dead horse, but the solution you're describing sounds a lot like Node-Webkit.

不要打败一匹死马,但您所描述的解决方案听起来很像Node-Webkit

From the Git Page:

从 Git 页面:

node-webkit is an app runtime based on Chromium and node.js. You can write native apps in HTML and JavaScript with node-webkit. It also lets you call Node.js modules directly from the DOM and enables a new way of writing native applications with all Web technologies.

node-webkit 是一个基于 Chromium 和 node.js 的应用运行时。您可以使用 node-webkit 用 HTML 和 JavaScript 编写本机应用程序。它还允许您直接从 DOM 调用 Node.js 模块,并支持使用所有 Web 技术编写本机应用程序的新方法。

These instructionsspecifically detail the creation of a single file app that a user can execute, and this portiondescribes the external dependencies.

这些说明专门详细说明了用户可以执行的单个文件应用程序的创建,这部分描述了外部依赖项。

I'm not sure if it's the exact solution, but it seems pretty close.

我不确定这是否是确切的解决方案,但似乎非常接近。

Hope it helps!

希望能帮助到你!

回答by Ian Tait

JXcore will allow you to turn any nodejs application into a single executable, including all dependencies, in either Windows, Linux, or Mac OS X.

JXcore 将允许您将任何 nodejs 应用程序转换为单个可执行文件,包括 Windows、Linux 或 Mac OS X 中的所有依赖项。

Here is a link to the installer: https://github.com/jxcore/jxcore-release

这是安装程序的链接:https: //github.com/jxcore/jxcore-release

And here is a link to how to set it up: http://jxcore.com/turn-node-applications-into-executables/

这是如何设置它的链接:http: //jxcore.com/turn-node-applications-into-executables/

It is very easy to use and I have tested it in both Windows 8.1 and Ubuntu 14.04.

它非常易于使用,我已经在 Windows 8.1 和 Ubuntu 14.04 中对其进行了测试。

FYI: JXcore is a fork of NodeJS so it is 100% NodeJS compatible, with some extra features.

仅供参考:JXcore 是 NodeJS 的一个分支,因此它 100% 兼容 NodeJS,并具有一些额外的功能。

回答by CoolAJ86

There are a number of steps you have to go through to create an installer and it varies for each Operating System. For Example:

要创建安装程序,您必须执行许多步骤,并且每个操作系统都会有所不同。例如:

回答by Nakedible

In addition to nexe, browserifycan be used to bundle up all your dependencies as a single .jsfile. This does not bundle the actual node executable, just handles the javascript side. It too does not handle native modules. The command line options for pure node compilation would be browserify --output bundle.js --bare --dg false input.js.

除了 nexe 之外,browserify还可用于将所有依赖项捆绑为单个.js文件。这不会捆绑实际的节点可执行文件,只是处理 javascript 端。它也不处理本机模块。纯节点编译的命令行选项是browserify --output bundle.js --bare --dg false input.js.

回答by HarrisonHymanson

You could create a git repo and setup a link to the node git repo as a dependency. Then any user who clones the repo could also install node.

您可以创建一个 git repo 并设置一个到节点 git repo 的链接作为依赖项。然后任何克隆 repo 的用户也可以安装 node。

#git submodule [--quiet] add [-b branch] [-f|--force]
git submodule add /var/Node-repo.git common

You could easily package a script up to automatically clone the git repo you have hosted somewhere and "install" from one that one script file.

您可以轻松地打包脚本以自动克隆您托管在某处的 git 存储库,并从一个脚本文件“安装”。

#!/bin/sh
#clone git repo
git clone your-repo.git