3

I have connected node inspector to my running node.js program. But I'd like to issue commands through the console - not necessarily on a breakpoint. For example, I have global variables and functions in my server.js program. For example:

var express = require('express');
var app = express();

function test() {
    console.log('yo');
}

app.listen(3000);

Now in node-inspector I go into the console and I type 'test()' and it returns "ReferenceError: test is not defined", same things with global variables I type 'app' and it tells me it's not defined. Any idea how to make this thing work? I just want to run my node program and then issue commands to it via a command line interface.

2 Answers 2

3

@foreyez,

Your question inspired me to make an npm module that lets you debug (via command line) a running node.js application without setting breakpoints or needing to make variables global:

https://github.com/go-oleg/debug-live

Hope that helps!

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

1 Comment

Awesome tool, thank you. console.log('yo'); doesn't output the string. Maybe you can improve this. And if I press the up and down keys, it doesn't navigate in command history. Syntax highlighting would be great as well!
1

The reason why it doesn't work as you expected is that all your variables and functions are local to your module (application) and you can't access them from a global context.

You can save them in the global scope if you want to access them from anywhere (including from the console when not stopped on a breakpoint):

var express = require('express');
var app = express();

function test() {
    console.log('yo');
}

app.listen(3000);

global.test = test;

4 Comments

doesn't node save them in a global context? like the window object in a browser?
No, they are stored in a closure. Look at the source code in node inspector, you will see that your code is surrounded by (function(...){ /* your code here */ }); block.
and there's no way for me to get inside that closure via command line without putting every variable inside global?
When you are paused in the debugger, then the console commands are run in the context of the current frame, i.e. you can access variables in closures that are visible from the function you are stopped in. But when the application is running, the console can access only globals.

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.