简体   繁体   English

NodeJS pkg如何指定主function

[英]NodeJS pkg how to specify main function

Basically I was doing some testing with the npm package named pkg This one基本上我正在用 npm package 命名pkg这个做一些测试

I was able to compile successfully and the program was running great if i were to run it through command prompt, but when i would just try double clicking the executable, i would get a command prompt window open for a split seconds and then close again, this is the code i tried testing it with如果我通过命令提示符运行它,我能够成功编译并且程序运行良好,但是当我尝试双击可执行文件时,我会得到一个命令提示符 window 打开几秒钟然后再次关闭,这是我尝试用它测试的代码

let prompt = require("prompt-sync")
async function run() {
    let result = await a()
    console.log(result)
    let b = await prompt.query("input test ")
    console.log(b)
}
run();

This all worked just fine when i ran the compiled app through command prompt, but doesnt work when i double click the app当我通过命令提示符运行编译的应用程序时,这一切都很好,但是当我双击应用程序时不起作用

What I've tried so far:到目前为止我已经尝试过:

  1. Tried different function names which may be used (such as main(), run(), etc.)尝试了不同的 function 可能使用的名称(例如 main()、run() 等)
  2. Tried different node targets尝试了不同的节点目标
  3. Tried looking at the v8 flags (though there was a lot so I might have missed some)尝试查看 v8 标志(虽然有很多,所以我可能错过了一些)

Any help is appreciated任何帮助表示赞赏

Okay, so after a bit of testing i found i can do the following:好的,所以经过一些测试后,我发现我可以执行以下操作:

  1. Make a while loop with while true and then to prevent many many many instances of the program opening because of the while true statement, i just define a variable outside the while true and then i put an if statement checking if the value is changed or not and if not it will run the run function otherwise it will just skip over.用 while true 创建一个 while 循环,然后为了防止由于 while true 语句而打开程序的许多实例,我只是在 while true 之外定义一个变量,然后我放一个 if 语句检查值是否已更改如果没有,它将运行运行 function 否则它将跳过。

 let i = 0; function run() { console.log("hi") } while (true) { if(i === 0) { run(); i++; } }

Like this it will just run the program once while still not just popping up for a split second and closing.像这样,它只会运行程序一次,而不会只是瞬间弹出并关闭。

This is not exactly what I wanted but this works the same way, which is good enough.这不完全是我想要的,但它的工作方式相同,这已经足够好了。

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

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