Skip to content

Instantly share code, notes, and snippets.

@mrlannigan
Last active February 23, 2018 17:08
Show Gist options
  • Save mrlannigan/a45c3e297b6864212f0b70ec54bb50aa to your computer and use it in GitHub Desktop.
Save mrlannigan/a45c3e297b6864212f0b70ec54bb50aa to your computer and use it in GitHub Desktop.
2018-02-23 [Brown Bag] Node.js Debugging and Profiling

Debugging in Node.js

Preparing the process

First step is to always use at least the latest LTS version of Node (8.x).

Next get acquainted with how to run your process without npm or yarn, for you will need to be able to run it with node process flags. For example, for looking-glass we start our server using an index.js file in the root of the project. So instead of running yarn start, we would run node index.js.

Now by default node doesn't have the "inspector" on in the process. You have to turn it on and there are a couple ways to do that. First, and the most common, is to pass a flag to the command; node --inspect index.js or node --inspect-brk index.js. Next, you can activate it by sending a SIGUSR1 to the running process; kill -s SIGUSR1 <pid>

If you have successfully entered debug mode, you will see some output to your stdout:

$ node --inspect .
Debugger listening on ws://127.0.0.1:9229/f3f6f226-7dbc-4009-95fa-d516ba132fbd
For help see https://nodejs.org/en/docs/inspector

Something to note about that url in the output. It's a websocket and Chrome supports ws:// as a protocol that can be navigated to.

At this point you have activated the debugger in node, it will remain in this mode until killed. This is important to remember if you are debugging a production instance of node, for you do not want to run the process in inspect mode in production.

Using Chrome Dev Tools

chrome://inspect

(Too much to write here)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment