node.js将REPL附加到脚本

时间:2021-08-20 21:59:23

How do I start a node.js script and still be able to execute commands into the terminal ? I am looking for a node.js REPL that is also there for my custom script, so that I can inspect/log the state of my program for instance.

如何启动node.js脚本并仍然能够在终端中执行命令?我正在寻找一个node.js REPL,它也适用于我的自定义脚本,因此我可以检查/记录我的程序状态。

This is something similar to this JVM question, but for node.js.

这与此JVM问题类似,但对于node.js.

I have tried node -i server.js without results. Do I need to have custom code in my script or is it feasible without that ? I saw this post, but it requires custom code, which I'd like to avoid.

我试过了没有结果的node -i server.js。我是否需要在我的脚本中使用自定义代码,否则它是否可行?我看过这篇文章,但它需要自定义代码,我想避免。

Also, bonus points for reattaching a node script launched by an init script (I can see it in the process list : node -i server.js).

此外,重新附加由init脚本启动的节点脚本的奖励积分(我可以在进程列表中看到它:node -i server.js)。

2 个解决方案

#1


0  

You can start a repl loop from within your program

您可以从程序中启动repl循环

http://nodejs.org/api/repl.html

#2


0  

Does the other way round work for you?

反过来对你有用吗?

Start the REPL and then load the script and then execute your commands. Use load to load your script.

启动REPL,然后加载脚本,然后执行命令。使用load加载脚本。

Inside REPL, try

在REPL里面,试试吧

.load server.js

#1


0  

You can start a repl loop from within your program

您可以从程序中启动repl循环

http://nodejs.org/api/repl.html

#2


0  

Does the other way round work for you?

反过来对你有用吗?

Start the REPL and then load the script and then execute your commands. Use load to load your script.

启动REPL,然后加载脚本,然后执行命令。使用load加载脚本。

Inside REPL, try

在REPL里面,试试吧

.load server.js