DEV Community

Cover image for Debug your Node.js app with Chrome DevTools
Benny Code for TypeScript TV

Posted on • Edited on

Debug your Node.js app with Chrome DevTools

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

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 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:9200 ./dist/app.js

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 (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.

TypeScript Debugging Setup

If you want to use Node's debugger for TypeScript code, you will have to compile your Node.js app to JavaScript. Make sure that the compiler option "sourceMap" in "tsconfig.json" is set to true in order to get source map support.

  1. Run npx tsc to compile your TypeScript app to JavaScript
  2. Run node --inspect ./dist/app.js to start your compiled code in watch mode for debuggers

When you are using ts-node, you can directly call:

  • node --inspect -r ts-node/register ./src/app.ts

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:

Get connected πŸ”—

Please follow me on Twitter or subscribe to my YouTube channel if you liked this post. I would love to hear from you what you are building. πŸ™‚ Best, Benny

Top comments (5)

Collapse
 
ganeles profile image
Ganeles Viktor • Edited

I wanted to create memory snapshot

I tried to connect to node application, and sometime it works but sometime not - I just don't see my devices by this link

chrome://inspect

I intercept traffic with wireshark and catch this error:
"WebSockets request was expected"

ok, I did the next:
1) Opened this link: localhost:9229/json
You will see something like this:

===================

[ {
  "description": "node.js instance",
  "devtoolsFrontendUrl": "devtools://devtools/bundled/js_app.html?experiments=true&v8only=true&ws=localhost:9229/4ae3df6d-2674-4daa-8090-e378b7b2af68",
  "devtoolsFrontendUrlCompat": "devtools://devtools/bundled/inspector.html?experiments=true&v8only=true&ws=localhost:9229/4ae3df6d-2674-4daa-8090-e378b7b2af68",
  "faviconUrl": "https://nodejs.org/static/images/favicons/favicon.ico",
  "id": "4ae3df6d-2674-4daa-8090-e378b7b2af68",
  "title": "/opt/testexec.server/hub/watchdog_service/app.js",
  "type": "node",
  "url": "file:///opt/testexec.server/hub/watchdog_service/app.js",
  "webSocketDebuggerUrl": "ws://localhost:9229/4ae3df6d-2674-4daa-8090-e378b7b2af68"
} ]
Enter fullscreen mode Exit fullscreen mode

===================

2) Opened the next link (devtoolsFrontendUrl):

devtools://devtools/bundled/js_app.html?experiments=true&v8only=true&ws=localhost:9229/4ae3df6d-2674-4daa-8090-e378b7b2af68

Where "4ae3df6d-2674-4daa-8090-e378b7b2af68" is ID from previous json

As result I got the access to profiler and took memory snapshot

Collapse
 
vjnvisakh profile image
Visakh Vijayan

Nicd

Collapse
 
andrewbaisden profile image
Andrew Baisden

Great post!

Collapse
 
deexter profile image
deexter

Thank you for the post, could you provide how to connect debugger with IDE like webstorm?

Collapse
 
zer0 profile image
zer0

There is an official documentation for it
jetbrains.com/help/webstorm/runnin...