How to debug in the back-end
Hello, I just realised that I never make use of debugging tools when it comes to web development. Every time I use console log statements and this might not be the best way to debug things especially if things get really complex.
I was reading a bit how to debug JS files in the front-end, I grasp the concept. But for back-end, when a server is running, is there a way to debug in these scenarios pls
25 Replies
Most of the time it's a simple
console.log statement, or console.error depending on the severity of the log. You really don't need to do too much more until you get to much larger scale.
It also depends on if you're debugging in your local dev environment or in production! In prod, you usually store your logs in a db (or perhaps just dump it to a file). So then you'll want to have some sort of structure to the logs so you can search for specific logs, trouble shoot based on frequency of errors, etc.yep, small question, what is the difference between a simple
console.log vs console.error? Do we have a use case for each pls
I was doing an internship some few months back, their I needed to "attached my process" or something like that I didn't really understand but it was some kind of debugging, do you have an idea of what it can be? I was using C# thoughThe console.log() static method outputs a message to the console.
–https://developer.mozilla.org/en-US/docs/Web/API/console/log_static
The console.error() static method outputs a message to the console at the "error" log level. The message is only displayed to the user if the console is configured to display error output. In most cases, the log level is configured within the console UI. The message may be formatted as an error, with red colors and call stack information.
–https://developer.mozilla.org/en-US/docs/Web/API/console/error_static
On the back end, console.log() outputs to standard out while console.error outputs to standard error
There's also the console.warn() method which is more than a log but less than an erroryeah I see
Source: Chrome dev console

for the stream standard error is it a file or it's still the screen?
Standard error is for Nodejs, not the web, so it's output to the standard error output…which is normally the terminal. Though many system daemons will dump
stderr to a fileto clarify a little:
stdout and stderr are the main two streams in a linux terminal environment. They're technically separate, but under normal operation both output to the terminal (screen).
When you run a command, you can redirect these outputs elsewhere. You may have seen cool_command -flags > output.log, which redirects stdout to the file output.log, but leaves stderr outputting to the console. It can be handy if you want to process the output of a command later, or need it for reference, to have just the golden-path output but still be warned out loud if something goes wrong.
stderr can be redirected to stdout too if you want, then you'd have > output.log 2>&1, which is a bit cryptic, but is effectively saying 2>(redirect second stream, stderr)&1(to the first one stdout)
If you're running node manually in the console, you'll get stdout and stderr printing to the terminal like any other command. console.error will show up in stderr like Beck said.
If you run node through something else, like pm2 or some other daemon, that daemon will receive the output. Where it redirects which stream is up to the daemon and how you configure it. pm2 for example logs each to a separate fileoh thanks for the explanation, small question
what do we mean by a "daemon"
is that the main process?
I saw this term often when I google it, it was telling me it's some kind of main thing, like daemon thread for instance which would mean main thread, I think
Also, to generalize a little, the first step in debugging backend is almost always printing to console or logging to a log file. Most bigger applications will have a logging system built in that you can dump info into for debugging purposes.
The next step is indeed to attach a remote debugger. PHP has one, I assume node does too, as does C#. This lets you set break points and examine the memory state on the backend server
daemon is, basically, a process that handles other processesthe daemon is the thing that starts node and restarts it if it crashes
It's a background service that runs and makes sure other things that should be running are running
I'm using
pm2 for a discord bot, but there's other options too
npm run technically is a Daemon too I supposeyeah I see
Most Linux distros ship with
systemd, for exampleThe next step is indeed to attach a remote debugger. PHP has one, I assume node does too, as does C#. This lets you set break points and examine the memory state on the backend serverOh ok make sense now what I attached a process in C#, will read a bit onto that, thanks !
Chrome can connect to a running Nodejs process to use its debugger
oh nice
oh
TIL
I guess I will have the opportunity to try that
will give that a read 🔥
Or more generally:
https://nodejs.org/en/learn/getting-started/debugging
Node.js — Debugging Node.js
Node.js® is a free, open-source, cross-platform JavaScript runtime environment that lets developers create servers, web apps, command line tools and scripts.