简体   繁体   English

如何使用zeit / pkg npm软件包将NodeJs项目打包为可执行文件?

[英]How to package NodeJs project into an executable with zeit/pkg npm package?

I am trying to make executable of my node app with https://github.com/zeit/pkg npm package. 我正在尝试使用https://github.com/zeit/pkg npm软件包使我的节点应用程序可执行。 I tried but not able to understand the given document completely or I'm doing something wrong. 我尝试但无法完全理解给定的文档,或者我做错了事。

After installing with the command 'npm install -g pkg' I have put the entry point in package.json file like: 使用命令“ npm install -g pkg”安装后,我将入口点放在package.json文件中,如下所示:

"bin": "app.js"

I'm running this cmnd: 我正在运行此cmnd:

pkg .

After this where I'll get the executable file that I can run. 之后,我将获得可以运行的可执行文件。 I tried running the file it creates with proj_name-win on windows but it's not working. 我尝试在Windows上运行使用proj_name-win创建的文件,但无法正常工作。 Can anybody explain me the steps to make executable and what I'm doing wrong? 谁能向我解释使可执行文件的步骤以及我做错了什么?

I found this example the most helpful (it includes the ability in package.json to dynamically bring in scripts and assets too): https://github.com/asaf050/loopback-pkg-ready 我发现这个示例最有帮助(它在package.json中也具有动态引入脚本和资产的功能): https : //github.com/asaf050/loopback-pkg-ready

I found these somewhat helpful too: http://thecodebarbarian.com/standalone-express-apis-binaries-with-pkg https://mrlithium.blogspot.com/2017/11/compiling-nodejs-app-into-exe-using-pkg.html 我发现这些也有帮助: http : //thecodebarbarian.com/standalone-express-apis-binaries-with-pkg https://mrlithium.blogspot.com/2017/11/compiling-nodejs-app-into-exe- using-pkg.html

Basically, "pkg ." 基本上是“ pkg”。 should work fine if your package.json is set up properly. 如果您的package.json设置正确,应该可以正常工作。 Note that it will build all 3 os's if you don't specify "-t latest-win-x64". 请注意,如果不指定“ -t Latest-win-x64”,它将构建所有3个操作系统。 You can also specify node specific options when running the server (eg --options expose-gc). 您还可以在运行服务器时指定特定于节点的选项(例如--options Exposure-gc)。 So a package might look like this: pkg -t latest-win-x64 . 因此,一个软件包可能看起来像这样:pkg -t Latest-win-x64。 --options expose-gc --options暴露gc

If you just use ".", then that should be the directory where your package.json is, and your package.json needs to have the bin and main entries, like you specified. 如果仅使用“。”,则该目录应该是package.json所在的目录,并且package.json需要具有bin和main条目,就像您指定的一样。 I'm not sure why the bin is needed since it's looks the same as main. 我不确定为什么需要该垃圾箱,因为它看起来与主要垃圾箱相同。 You can also specify the main file when calling pkg, like this: pkg -t latest-win-x64 ./server/server.js --options expose-gc 您也可以在调用pkg时指定主文件,如下所示:pkg -t Latest-win-x64 ./server/server.js --options Exposure-gc

Again, "-t" and "--options" are optional, all you need is to call "pkg ." 同样,“ -t”和“ --options”是可选的,您只需要调用“ pkg”即可。 or "pkg ./app.js" to build the package. 或“ pkg ./app.js”来构建软件包。

Can you post the error message you receive? 您可以发布收到的错误消息吗? Rather than clicking the .exe, call if from command line to get the logs, or output to a log file. 而不是单击.exe,而是从命令行调用if以获取日志,或将其输出到日志文件。 For me, the reason it was not running was due to static file and dynamic module loading, which it specified in the build and when running the .exe. 对我来说,它没有运行的原因是由于静态文件和动态模块加载(在构建中以及运行.exe时指定)。

Please add the below code to work with pkg : 请添加以下代码以使用pkg:

package.json package.json

"main": "app.js",
"scripts": {
"start": "npm start"
 },
"bin" : "$DIR\node_modules\npm\cli.js"

Then run command pkg . 然后运行命令pkg。

Note : 注意事项

1) You can also directly package app.js (in case if you dont have any other assets to add in pkg) by command : 1)您也可以通过以下命令直接打包app.js(以防您没有其他要添加到pkg中的资产):

pkg app.js

2) It will create executables in your current working directory. 2)它将在您当前的工作目录中创建可执行文件。

3) You can also give entrypoint in "bin" : 3)您也可以在“ bin”中指定入口点:

Example : 范例:

package.json package.json

"main": "app.js",
"scripts": {
"start": "npm start"
 },
"bin" : "./bin/my-bin.js"

bin/my-bin.js bin / my-bin.js

#!/usr/bin/env node

var join = require('path').join
var childProcess = require('child_process');
var args = process.argv.slice(2);

 args.unshift(__dirname + '/../'); 

childProcess.exec('npm start', (err, stdout) => {
if (err) console.log(err);
console.log(stdout);
})

Then, in current working directory, 然后,在当前工作目录中

run

pkg .

似乎pkg并没有得到更多维护,我建议研究一下nexe

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM