Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running console commands through node inspector?

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.

like image 690
Shai UI Avatar asked May 11 '26 22:05

Shai UI


2 Answers

@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!

like image 108
go-oleg Avatar answered May 14 '26 13:05

go-oleg


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;
like image 22
Miroslav Bajtoš Avatar answered May 14 '26 13:05

Miroslav Bajtoš



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!