Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to programmatically detect debug mode in nodejs?

I've seen this question asked of other platform/languages - any ideas? I'd like to do something like:

if (detectDebug()) {     require('tty').setRawMode(true);         var stdin = process.openStdin();      stdin.on('keypress', function (chunk, key) {         DoWork();     } } else {     DoWork(); } 

I'd like to be able to toggle keyboard input as a start for the script when debugging so that I can have a moment to fire up chrome to listen to my node-inspector port.

***Quick update - I'm guessing I can actually use "process.argv" to detect if --debug was passed in. Is this the best/right way?

like image 609
j03m Avatar asked Jul 31 '11 12:07

j03m


2 Answers

NodeJS creates a v8debug global object when running in debug mode: node debug script.js

So, a possible solution would be:

var debug = typeof v8debug === 'object'; 

For my use case, I use it because I want to avoid passing environment variables. My main node process starts child node processes and I want a node debug mainScript.js to trigger debug mode for children as well (again, without passing env variables to child processes)

like image 90
4 revs, 2 users 76% Avatar answered Oct 08 '22 12:10

4 revs, 2 users 76%


I use this

var debug = typeof v8debug === 'object'              || /--debug|--inspect/.test(process.execArgv.join(' ')); 

which supports, --debug, --debug-brk, --inspect and --inspect=1234

like image 20
bentael Avatar answered Oct 08 '22 12:10

bentael