2

If I have a node.js web server running with express, is there a way to get a command line? Kind of like the way the browser has a javascript console. I want to type in variables and inspect them in node.js runtime.

If I type in 'node' then it gives me a command line. but if I type in 'node server.js' that command line is not available. How can I make the command line always available (in bottom of screen) regardless of the server's state?

3
  • You really have two options. 1: Open another terminal. 2: Use something like pm2 (an npm package) that runs a script in the background and frees up your command line. I use pm2 on all my node servers. npmjs.com/package/pm2 Commented Jan 21, 2021 at 16:31
  • 1
    @JakeAve the question is asking about opening a console to inspect variables at runtime, not about having another terminal open (or not being able to use the terminal while the express app is running) Commented Jan 21, 2021 at 16:33
  • That makes more sense! I have never heard of anything besides like the vs code debugger that lets you see variables at runtime (unless you want to add console logs all over your server), but I'll follow this question and see if something else is out there. Commented Jan 21, 2021 at 16:38

1 Answer 1

1

To do this type node --inspect server.js

Then to connect to it type in chrome://inspect/ in url bar of Chrome. Then click "inspect" on that instance under Remote Target. Then I found I can see variables/objects that are under the global namespace.

Also, a very basic way to add keyboard interaction on the server window itself without having to connect to it:

var readline = require('readline');
readline.emitKeypressEvents(process.stdin);
process.stdin.setRawMode(true);

setInterval(function() {
    console.log('server stuff');
}, 1000);

process.stdin.on('keypress', (str, key) => {

    if(key.sequence === '\u0003') {
        console.log('ctrl-c pressed');
        process.exit();
    }

    console.log('you pressed: ' + key.name);
});
Sign up to request clarification or add additional context in comments.

1 Comment

if using nodemon make sure to start it with -I or setRawMode won't work.

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.