58

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?

1
  • The "process.argv" does not help you when you call: node debug script.js. Also when you use --debug, the execution does not stop at "debugger" instructions. Nov 19, 2012 at 12:59

12 Answers 12

53

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)

3
  • 5
    thanks, this also better answers the question (which is not "how can i set production mode")
    – JasonS
    Jul 15, 2013 at 2:04
  • 6
    Per @Alex it appears the global does not exist in newer versions of Node, including the latest LTS. Dec 8, 2017 at 21:45
  • 13
    This has not been working since several major Node versions ago. Mar 15, 2019 at 11:30
32

I use this

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

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

8
  • 3
    Good answer. When testing my vscode extension v8debug is not present, so the top answer would not work for me. But --debug-brk is there so your code caught it :)
    – Almenon
    Nov 18, 2018 at 18:30
  • 1
    This worked when I changed process.execArgv to process.argv when debugging using VS Code. Apr 24, 2019 at 20:10
  • 3
    for those using TypeScript, remember to prepend v8debug with global.v8debug. Please keep in mind that newer versions of NodeJS do not even set v8debug, so that's why this answer that I'm commenting on is the best since it catches more modern versions of NodeJS. Aug 15, 2019 at 12:57
  • This doesn't work generally since a debugger can be attached after the process has started Aug 15, 2019 at 17:50
  • 1
    @bentael yes - we are discussing this internally in core: github.com/nodejs/node/issues/9617#issuecomment-521955240 Aug 23, 2019 at 15:08
28

The global v8debug variable mentioned in the other answers seem to be removed in Node v7.0.0 (see https://github.com/nodejs/node/issues/9617). Also, checking process arguments (i.e. process.execArgv) seem to be unreliable since Node can enter debug mode at runtime. VS Code, for example, doesn't always starts Node with the --inspect option even when debugging. (depend on your debug config)

The most reliable solution I could find is to use inspector.url() to check if Node is listening for debug connections.

const inspector = require('inspector');

function isInDebugMode() {
    return inspector.url() !== undefined;
}

I have tested this method with Node versions v12.22.1, v14.16.1, and v16.1.0 and it worked for all of them.

25

There is a node.js native support that using inspector.url() to check if there is active inspector, it just shows if process is debug mode or not currently. See doc for more.

1
10

I think there's a bunch of confusion in this question.

Based on your question, I think what you really want is the node --debug-brk command line flag. That will have node start v8 with a debugger running and automatically stop on a breakpoint before the first line of your .js program. You don't need to reinvent this. I use this for debugging mocha tests, express.js startup issues, etc. This will eliminate your need to detect this manually.

Secondly, NODE_ENV=production is nothing more than a convention that many programs use to indicate "you are running in production" and therefore certain things like really sending emails, using the real payment gateway, etc should be enabled. However what environment you are in when NODE_ENV is NOT production (or is unset) should definitely NOT be assumed to be debugging. The most sane assumption there is that the environment is a development environment, but even then this whole convention is pretty brittle in my opinion.

Thirdly, just FYI check out tty.isatty() which will accurately tell you if your program is being run on an interactive terminal (like from a command shell). This will be false when your program is being run by a process supervisor provided by your OS (upstart, sysvinit, etc). This check is commonly use to toggle command line programs between interactive and scripted modes. It's not entirely perfect or infallible, but it is widely adopted in the posix world.

Fourth, from some quick experimentation, the v8debug global @Gabriel Petrovay indicates seems to only be set when doing node debug script.js and not set when doing node --debug script.js. Not sure why that is. If such a thing was reliable, that would seem like the most correct approach to finding out "is this v8 instance in debug mode".

0
6
var detectDebug = function() {
    return process.env.NODE_ENV !== 'production';
};

to run in debug mode:

$ node app.js

to run in production mode:

$ NODE_ENV=production node app.js

Some frameworks recognize the production mode this way. See express.js doc.

2
  • This seems to work with electron apps, but the values are not intuitive. When running with npm start process.env.NODE_ENV is 'production' but when the app is packaged into a stand-alone executable the variable is undefined.
    – Jason
    Feb 7, 2016 at 18:02
  • 1
    This is brittle and inconsistent. There's far better ways to do this in this thread.
    – jcollum
    Feb 9, 2016 at 19:25
5

The global.v8debug object only seems to be created / exposed when the debug or --debug-brk command line option is set. It's strange and annoying it's not created when --debug is set.

A hacky way to do this would be to look at the process.execArgv array (not process.argv) for --debug, --debug-brk or debug.

4
  • you would think... but the --debug command line flag is consumed before it gets to us Nov 3, 2014 at 15:14
  • 2
    seems to be available in process.execArgv instead Nov 3, 2014 at 15:25
  • I think that with --debug its only listening for a debugger to attach. With the other cases the debugger actually attaches, thus the v8debug variable is set. Jan 21, 2016 at 21:01
  • In both cases, you need to manually attach a debugger. With --debug-brk, it will break execution on the first line of user code, whether a debugger is attached or not. Jan 22, 2016 at 0:07
4

process.debugPort appears to always be present and defaults to 5858. The only way to detect if the program was started explicitly with --debug is to check the process.execArgv array. --debug-brk is pretty obvious to detect: your program won't do anything and you'll get a message about a debugger listening, so that's trivial to figure out. v8debug appears to be present when the program is started with node debug file.js or when a debugger like node-inspector is currently attached.

Keeping all that in mind, this code will detect whether a debugger (of any kind) is currently attached.

 var debug, withDebug;

  debug = false;

  if (typeof v8debug !== "undefined" && v8debug !== null) {
    console.log("v8 debug detected");
    debug = true;
  }

  withDebug = process.execArgv.indexOf('--debug') > -1 || process.execArgv.indexOf('--debug-brk') > -1;

  if (withDebug) {
    console.log("started with debug flag, port: " + process.debugPort);
    debug = true;
  }

  if ((typeof v8debug === "undefined" || v8debug === null) && !withDebug) {
    console.log("neither detected");
  }
5
  • 1
    Keep in mind that --debug is actually the really ancient debugger, modern Node.js debugging nowadays is actually done using --inspect which allows you to use the Chrome DevTools. Jan 31, 2017 at 11:35
  • Right, I wasn't aware of --inspect when I wrote this; if I run node --inspect --debug start.js is the --debug redundant?
    – jcollum
    Jan 31, 2017 at 21:41
  • Yeah --debug will be removed in version 8.0.0 of node, which isn't that far off as we are on 7.4 -- actually latest is 7.5.0 now. node --inspect was added with 7.0.0, I think, which was 3 months ago. Feb 1, 2017 at 15:24
  • 1
    By the way - I am using const isInspected = process.execArgv.includes('--inspect'); in an application at the moment, to disable syntax colouring – so that my devtools console logs aren't overwhelmed with ANSI terminal specific syntax for colours (which don't work in the DevTools console and require different approach). Feb 1, 2017 at 15:31
  • be care, process.execArgv.includes('--inspect') will not work for --inspect=7000 or --inspect-brk=7000
    – atian25
    Mar 29, 2017 at 7:22
2

There is no v8debug in versions of nodejs >=7. It seems that the most simple way is to check command line parameters. In debug mode there will be a word 'debug' or 'inspect'

const argv = process.execArgv.join();
const isDebug = argv.includes('inspect') || argv.includes('debug');
1
2

There simple solution is here.

But in general, detect debug mode is not easy - https://github.com/nodejs/node/issues/9617

1
  • 1
    This is a combination of (non-)answers, better left as individual comments. Aug 17, 2021 at 15:08
0
Object.defineProperty(global, 'isDebugging', {
    get: function () {
        return typeof v8debug !== 'undefined';
    }
});

This works well for webstorm

3
  • Doesn't work if you start the process at the command line with --debug.
    – jcollum
    Feb 9, 2016 at 19:30
  • I don't see why it wouldnt?
    – Georgi-it
    Feb 10, 2016 at 10:50
  • As pointed out in other answers, v8debug is no longer defined in Node later than v7. Mar 18, 2019 at 5:05
0

I had the same issue, how to check the app to see if it is running on --inspect.

I m on nodejs 14, and the solution with the v8debug that mentioned above, looks that it does not work any more, for clarity i post the message that i get when i try --debug.

node: [DEP0062]: node --debugandnode --debug-brkare invalid. Please usenode --inspectandnode --inspect-brk instead.

Solution

So how i tackle with this, is to query the process.execArgv which is exactly what you need too.

snipet:
const isInspect = process.execArgv.join() === '--inspect' //or ....process.execArgv.toString() === ...

This execArgv, as the docs state:
The process.execArgv property returns the set of Node.js-specific command-line options passed when the Node.js process was launched. These options do not appear in the array returned by the process.argv property, and do not include the Node.js executable, the name of the script, or any options following the script name. These options are useful in order to spawn child processes with the same execution environment as the parent.
More in the docs: https://nodejs.org/docs/latest-v6.x/api/process.html#process_process_execargv

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.