0

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.

This is something similar to this JVM question, but for 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.

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).

Community
  • 1
  • 1
nha
  • 17,623
  • 13
  • 87
  • 133

2 Answers2

1

You can start a repl loop from within your program

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

  • Like in this post : http://stackoverflow.com/questions/9302458/node-js-hooking-repl-to-a-remote-node-server but that requires custom code, right ? Is it possible with an already running node process ? – nha Mar 04 '15 at 10:29
  • 1
    As far as I know it is not possible to attach a repl to an already existing process if that is what you are asking. The code in that answer creates a server which serves a repl when someone connects. You could connect and disconnect from that multiple times(since the server will just keep listening). – Brent Nkomo Mar 04 '15 at 10:34
  • Yes attach a REPL to an existing process is what I am asking. I'm not sure it is possible though. – nha Mar 04 '15 at 13:47
  • I will accept your answer for now, but I would still be happy to find something that does this without having special code. – nha Jun 23 '15 at 13:19
1

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.

Inside REPL, try

.load server.js
Anurag Peshne
  • 1,547
  • 12
  • 29
  • My first thought is that if an extra bootstrap step is needed, I would prefer it in the code / startup script. Thank you though for the .load method, it looks interesting. I'll think about it. – nha Mar 04 '15 at 13:45