Urgent.News

What's breaking now, across thousands of outlets.

Tech

Debugging Node.js Like a Pro

Most Node.js debugging happens with console.log . That works until it doesn't: async stacks that point to the wrong place, a variable that changes between the log and the crash, or a server that only misbehaves on one request out of a thousand. Here's the toolkit I reach for instead. Start with --inspect , not print statements The built-in inspector gives you breakpoints, step-through, and a real…

Debugging Node.js applications can be challenging, especially when dealing with async stacks, variables changing between logs and crashes, or sporadic misbehaviors. The recommended approach is to start by using the built-in inspector with `node --inspect app.js` and then open Chrome's `chrome://inspect` to connect. For processes that are difficult to restart, such as workers or Docker containers, use `--inspect-brk` to pause on the first line or send `SIGUSR1` to a running process to enable the inspector.

Breakpoints should be set conditionally rather than on every iteration, and `debugger;` statements can be used temporarily in production code. It's also important to log objects properly, such as using `console.log(JSON.stringify(user, null, 2))` or `console.dir(user, { depth: null })`. For async issues, enable `--async-stack-traces` to see the full causal chain of errors.

Finally, if an application is slow rather than broken, use the built-in profiler with `node --cpu-prof` or `--heap-prof` to identify bottlenecks in CPU usage or memory allocations, respectively.

Brief written by urgent.news from Dev.to's own syndicated text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Tech

More from Thursday 17 September →