16

The console.debug() function can be invoked in the browser console.

However there is one error when console.debug() is called in Nodejs.

TypeError: Object #<Console> has no method 'debug'
    at Object.<anonymous> (c:\share\node\receive.js:20:9)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)
    at node.js:929:3

Why? Is there any way to replace console.debug in Nodejs?

zangw
  • 43,869
  • 19
  • 177
  • 214

6 Answers6

19

There's no console.debug() method in NodeJS. Here is the documentation for the console object, so you can choose the best method for you to use.

Morry
  • 726
  • 4
  • 11
  • 10
    Note that `console.debug()` is present in Node v8 (and possibly v7), but it appears to be a no-op: the function invokes without error, but no content comes out on stdout or stderr. – Phrogz Jun 23 '17 at 17:21
  • Damn, that broke my library. – unional Nov 04 '17 at 19:20
  • 1
    [documentation](https://nodejs.org/api/console.html#consoledebugdata-args): *v8.10.0 console.debug is now an alias for console.log.* – Nagev Dec 14 '21 at 12:51
6

As the previous answer states there is no console.debug() method in node.js. use log,info,warn,error methods https://nodejs.org/api/console.html

That said you might what to extend the console object to include a console.debug() method and only have this method print console messages when in a debug mode.

var isDebugMode = true;

console.debug = function(args)
{
  if (isDebugMode){
    console.log(args);
  }
}
maninvan
  • 890
  • 9
  • 10
  • 1
    Note that `info` is just an alias for `log`, and `warn` is just an alias for `error`. Unlike what the names imply, in Node you cannot set various levels of logging. – Phrogz Jun 23 '17 at 17:21
5

Since node 9.3.0 console.debug will now show by default in stdout.


Old solution:

console.debug() was added in node v8.0.0, but will not normally print to stdout.

You can view console.debug messages by using the --inspect or --inspect-brk command line flags and enabling verbose logging with the "levels" dropdown in devtools' console tab.

devtools' console tab with default filtering devtools' console tab with no filtering

Unfortunately this does not unsquelch console.debug's output to stdout.

stdout in repl

As of node v9.2.0 i'm unaware of any way to view console.debug's output in stdout short of replacing the function with a console.log wrapper.

Kevin
  • 525
  • 4
  • 8
4

Adding on to @maninvan answer, I'd use variables args syntax:

var isDebugMode = true;

console.debug = function(/* ...args */) {
    if(isDebugMode) {
        var vargs = Array.prototype.slice.call(arguments);
        console.log.apply(this, vargs);
    }
}

// or ES6 style
console.debug = (...args) => {
    if(isDebugMode) {
        console.log.apply(this, args)
    }
}
binarygiant
  • 6,362
  • 10
  • 50
  • 73
0

With node 8.10.0 and above support has been added for console.debug()

https://github.com/nodejs/node/commit/162ff56439

Ankit
  • 520
  • 7
  • 21
0

As the documentation history says:

v8.10.0 console.debug is now an alias for console.log

While it's easy to overwrite it with your own function, I prefer to extend console and use my own debug method, like in this example:

xconsole = Object.create(console);

xconsole.debug = function (...line) {
        if (process.env.NODE_ENV === "development") {
                console.log(...line);
        }
}

This leaves console.debug untouched. I have this in its own file which I import to use in other modules. And I can use the new function instead, like this:

xconsole.debug("This will only show if NODE_ENV is set to development");
xconsole.log("This will always show");

Many variations are possible, of course. See info on NODE_ENV.

I use this approach to conveniently enable debug mode on docker containers. I have an alias on my ~/.bashrc to propagate the environment variables since I need to run docker with sudo:

alias docker='sudo -E docker'

And if I need to enable debug mode it's as simple as export NODE_ENV=development. Testing:

docker run --name test -d -e NODE_ENV node:17-bullseye env

I can see NODE_ENV=development with docker logs test. It's good to be mindful of security when exporting environment variables to root (-E), but this is fine for my development environment.

Nagev
  • 10,835
  • 4
  • 58
  • 69