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.