2

Is there a way to run commands while a Node.js server is live (after it's been started) and access all functions/variables/etc?

For example,

after running node server.js, is there a way I can execute a function or run commands in the live environment via command-line?

2 Answers 2

2

You can start a REPL at any time using the repl core module. With useGlobal you can make the global scope shared, and using context you can pass in whatever local context you need:

import repl from 'repl'
const r = repl.start({ useGlobal: true })
r.context.someStuff = 123 // will be accessible as someStuff in REPL

Then you get the usual prompt in your console and you can interact with it to run commands and view variables. It runs in the same process as your application, so you can import/require modules and access their exports, etc. and their data will be shared with the rest of your code.

If you want to insert this at some specific point in your code and get full access to local variables there, you can use a custom eval function that calls JavaScript's eval in the local scope:

repl.start({
  eval: (cmd, context, filename, callback) => {
    with (context) callback(null, eval(cmd))
  }
})

(Note: With this, defining new variables with let will not work across commands.)

To also get history in the programmatic REPL, you can take a look at repl-story or similar packages.

If you want to access a similar console through a web browser remotely (careful - security risk - best to make available only via SSH port forwarding), take a look at node-browser-repl.

In case you are looking a full debugger experience though, check out T.J. Crowder's answer about how to attach DevTools to a node.js application.

Sign up to request clarification or add additional context in comments.

4 Comments

Amazing! I'm using a cluster so I added it to the the Master and it worked like a charm!
I think in this case you won't be able to access the data of the individual instances though, only of your master. You would probably have more luck adding the browser REPL thing and listening on different ports for different instances, so you can differentiate.
Yeah, I tried adding the repl.start to the child processes but it creates a repl instance for each child which is not appropriate and is not functional because typing a command executes that command to all repl instances simultaneously.
FYI I was able to solve this by only starting repl for the first Worker --> if (cluster.worker.id === 1) repl.start({ useGlobal: true }) . Now I can access the repl within one of the Workers which is exactly what I needed. Thank you!
2

Basically, it sounds like you're looking for a Node.js equivalent of the browser console that's available to browser-hosted JavaScript.

Node.js doesn't provide one directly (it does have a REPL that may serve your needs; see CherryDT's answer), but many IDEs include a "Debug Console" (that's what VS Code calls it) or similar that you can use when running the code in debug mode. You can inspect global variables (of course, you tend not to have very many global variables in Node.js apps, which are inherently modular), call global functions (same), and more to the point interact with the current scope when paused on a breakpoint.

You can also run Node.js directly with a debugging flag that tells it to allow a debugger to attach to it. For instance:

node --inspect-brk server.js

...tells Node.js to run with debugging enabled. You'l see something like:

Debugger listening on ws://127.0.0.1:9229/89eff679-76ac-4972-9bd9-3142214c0b1f
For help, see: https://nodejs.org/en/docs/inspector

If you open a Chromium-based browser and go to the URL chrome://inspect, you'll see your target listed:

Target (v16.13.2)    trace
index.js
file:///path/to/server.js
inspect

...where inspect is a link. Click it, and devtools will open, showing your code and that the execution is stopped on an automatic breakpoint at the very beginning of the code. (You can use --inspect instead of --inspect-brk if you don't want to start out at a breakpoint.)

2 Comments

A tip that's good to know: If you realize you need the debugger when the server was already started without this flag, you can send a SIGUSR1 to the process to enable debug mode "in-flight" (kill -SIGUSR1 <process ID here>). Also, if this is running on a remote server, you can use SSH to forward port 9229, then you will be able to access it with your local devtools as well.
@CherryDT - Cool!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.