Home > front end >  Running commands while a server is live?
Running commands while a server is live?

Time:04-30

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?

CodePudding user response:

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.

CodePudding user response:

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

  • Related