zangw
zangw

Reputation: 48526

Console has no method 'debug' in Nodejs?

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?

Upvotes: 16

Views: 18739

Answers (6)

Nagev
Nagev

Reputation: 13295

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.

Upvotes: 2

Ankit
Ankit

Reputation: 540

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

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

Upvotes: 1

Kevin
Kevin

Reputation: 544

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.

Upvotes: 5

binarygiant
binarygiant

Reputation: 6422

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)
    }
}

Upvotes: 4

maninvan
maninvan

Reputation: 920

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);
  }
}

Upvotes: 6

Morry
Morry

Reputation: 736

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.

Upvotes: 19

Related Questions