Node.js debugging

From mediawiki.org

Stack overflows[edit]

It used to be hard to debug stack overflows in node, as it would just bomb without producing a stack trace. This has thankfully changed with node 0.11. It also helps to increase the stack depth to spot larger recursions:

$ node --stack-trace-limit=1000 <script.js>

Chrome DevTools[edit]

See also: Debugging Node.js Nightlies with Chrome DevTools. Available as of Node v6.3.0.

$ node --inspect script.js

  To start debugging, open the following URL in Chrome:
  chrome-devtools://devtools/remote/serve_file/@../inspector.html?..

For command-line scripts that run immediately, you may want to use --debug-brk so that you have an opportunity to start a Profile in the Dev Tools, for example. (Starting a CPU profiler will automatically resume script execution).

For services you'll usually want to omit --debug-brk so that it won't time out when creating workers. Attach your debugger in Chrome, start a Profile, and then make requests to your service. Here's an example profiling Parsoid. We set worker count to 1 as each process gets its own debugger.

$ node --inspect ./node_modules/.bin/service-runner -n 1

  Debugger listening on port 9229.
  To start debugging, open the following URL in Chrome:
    chrome-devtools://devtools/remote/serve_file/@../inspector.html?..&ws=localhost:9229/node

  Debugger listening on port 5859.
  To start debugging, open the following URL in Chrome:
    chrome-devtools://devtools/remote/serve_file/@../inspector.html?..&ws=localhost:5859/node

Made a request to "http://localhost:8000/localhost/v3/page/html/Main_Page" and stopped the debugger. Example flame chart: https://i.imgur.com/HshfDOx.png

Profiling[edit]

A good tool for profiling nodejs applications is the combination of nodegrind and kcachegrind.

Quick start:

$ npm install -g nodegrind
$ nodegrind <script.js>
  Profile written to "callgrind.out.8004"
  Try `kcachegrind "callgrind.out.8004"`
$ kcachegrind callgrind.out.8004