DEV Community

loading...
Cover image for How do I debug my Node.js application in Chrome DevTools

How do I debug my Node.js application in Chrome DevTools

Sagar Barapatre
19 | A fool with a chai (tea) mug and a laptop, who is trying to get some fun out of it
・2 min read

You can use Chrome's DevTools to debug Node.js applications. It's called remote debugging and can be started from Google Chrome's internal website.

Setup your Node.js app for debugging

To inspect your Node.js app with Google Chrome DevTools, you have to make it a remote target. This can be done by using the --inspect flag when starting the node process:

$ node --inspect ./dist/app.js
Enter fullscreen mode Exit fullscreen mode

Use Chrome DevTools

Once your Node.js app is registered as remote target and listening for debuggers, you can open the webpage

chrome://inspect/#devices
Enter fullscreen mode Exit fullscreen mode

in Google Chrome to see the dedicated DevTools for Node.

Set IP and Port

By default, the DevTools try to discover targets on "127.0.0.1:9229". You can change the host and port. Make sure that your Node.js process is listening to it. You can point Node.js to a specific IP and Port by running:

node --inspect=127.0.0.1:3000 ./dist/app.js
Enter fullscreen mode Exit fullscreen mode

If you want to allow external connections (from the public internet) you have to bind the debugging interface to IP/Host "0.0.0.0".

Use CLI Debugger

If you want to run debugging entirely in the CLI, you can start your app with:

node inspect ./dist/app.js
Enter fullscreen mode Exit fullscreen mode

(Note the missing hyphens!)

Be aware that the Node.js inspector supports breakpoints but is not a full-featured debugger. If you want to continue from a breakpoint (set with the debugger statement), you have to enter cont (continue) within the CLI.

Pause Debugging

When your app has a heavy initialization, you may want to pause your app until the debugger is attached. This can be done by using the flag --inspect-brk, which sets a break before running your code. You can use your remote debugger (i.e. Chrome DevTools) to unpause the debugging process.

Debugger doesn't stop with TypeScript

When your JavaScript code uses debugger statements but your code doesn't stop, then it is very likely that your debugger keyword got removed during compilation. You can prevent JavaScript's compiler from removing the debugger keywords by using this code comment:

debugger; // (tsc-save)
Enter fullscreen mode Exit fullscreen mode

Video Tutorial

The video below, will help you with the Node.js debugging setup:

Following the YouTube link, you will also find the timeline for each chapter.

Discussion (0)

Forem Open with the Forem app