73K Followers
·
Follow

Debugging Node.js with Chrome DevTools

Support for Node.js debuggability landed in Node.js in 2016. Here’s how to get up and running. (Post updated Jan 2018)

1. Download and install the current version of node. (v6.3.0+ required)

2. Run node with the --inspect flag:

node --inspect index.js
# Break on the first statement of the script with --inspect-brknode --inspect-brk index.js

(In Node 6, you have to use --inspect --debug-brk for this inspect & pseudo-breakpoint combo.)

Next, you used to open the big chrome-devtools:// URL it spits out, but don’t. Now there’s a better way…

3. Open about:inspect in Chrome

It’ll redirect you to chrome://inspect quickly and you’ll see something like:

Image for post
Image for post
chrome://inspect screenshot

4. Click the Open dedicated DevTools for Node link.

Youll get a popup window for debugging your node session.

But better than that, when you kill and restart node, the window will automatically reconnect to it. 🔁🔁💥

(Btw: the inspect link beneath the specific target will only apply for that session of node and won’t reconnect.)

Image for post
Image for post

In DevTools, now connected to Node, you’ll have all the Chrome DevTools features you’re used to:

  • Complete breakpoint debugging, stepping w/ blackboxing
  • Source maps for transpiled code
  • LiveEdit: JavaScript hot-swap evaluation w/ V8
  • Console evaluation with ES6 feature/object support and custom object formatting
  • Sampling JavaScript profiler w/ flamechart
  • Heap snapshot inspection, heap allocation timeline, allocation profiling
  • Asynchronous stacks for native promises

To see this in action, here’s my demo during the 2017 Node Summit:

Lastly, there is now official documentation on debugging Node with the new protocol: https://nodejs.org/en/docs/inspector/

Written by

The web, performance, and tooling. Also ❤z.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store