Let's Debug Node.js Applications

Julián Duque

Lead Developer Advocate - Salesforce Heroku

@julian_duque

https://twitch.tv/julianduque

Mohith Shrivastava

Lead Developer Advocate - Salesforce

@msrivastav13

Challenges

  • Debugging asynchronous workflows add a layer of complexity
  • Post-mortem debugging requires C/C++ tooling skills

How to debug Node.js Applications?

Choose your weapon!

Node Inspect

  • node --inspect <script>
  • node --inspect-brk <script>
  • Go to

    • chrome://inspect
  • Click on Inspect
 
 

Node Inspect

From Chrome DevTools you can debug your Node.js application by adding breakpoints, controlling the step-by-step execution, and explore variables and call stack

 

Node Inspect

Instead of adding console.log you can add Logpoints as if they were breakpoints and they will be logged during debugging.

Node Inspect

From DevTools you can also profile the memory of your application to learn more about allocation, and hunt down memory leaks.

Some tips

  • Start to debug on a running process with:
    • kill -USR1 <pid>
  • Add a breakpoint to your code using a JS keyword:
    • debugger;

VSCode

Visual Studio Code supports the V8 inspector protocol and it is a great tool for local and remote debugging.

Other IDEs like WebStorm and Atom also support this protocol, use the tool you are most used to.

Some tips

  • You can also add Logpoints from VS Code
  • You can have multiple debugging configurations

ndb

It is a Chromium-based, standalone Node.js debugger by the ChromeLabs team with extra features that Chrome Inspector doesn't support out of the box

  • Child Process Debugging
  • Blackboxing
  • Edit in Place
$ npm install -g ndb

llnode

  • Useful for Post-Mortem debugging
  • Get useful information from a coredump after a crash
  • It helps to see the state of the Node process and debug the C/C++ native layer

 

  • npm install -g llnode
  • llnode `which node` -c core.dump

 

Useful npm modules

debug

Allows you to have a console-based debugger that can be enabled by using an environment variable

 

$ npm install debug

trace & clarify

trace: Augment your async stack traces

clarify: Removes node internals

$ npm i --save trace clarify

$ node --stack_trace_limit=100 -r trace server.js

$ node --stack_trace_limit=100 -r trace -r clarify server.js

Thank you!