Skip to content

Node.js debugging

Joe Cheng edited this page Oct 19, 2016 · 3 revisions

Starting with Shiny Server 1.5 (and its move to Node v6), it's possible to use an interactive Node.js debugger while developing or debugging Shiny Server. There are (at least) three ways to do so.

Using --debug

The node executable takes a --debug flag to start Node in debugging mode (or use --debug-brk to make Node pause execution before executing user code). In order to run Shiny Server in this way, you can't use the usual shiny-server binary, and must instead call node directly. Assuming you are in the git repo (or /opt/shiny-server):

sudo bin/node --debug lib/main.js [args...]

You can now use an interactive debugger like the one in Visual Studio Code to step through the JavaScript. To attach Visual Studio Code to a root process, you must use sudo to start Visual Studio Code as well.

sudo code --user-data-dir=[path-to-home-dir]
  • Press Ctrl+Shift+D to navigate to the Debug tab.
  • At the top, you'll see ▶, "Launch" and ⚙. Click on ⚙ and select Node.js.
  • Now the "Launch" drop-down should have an option for "Attach to process". Choose that.
  • Click ▶ and choose your node process.

Your debugger is now attached.

Using --inspect

Another way to debug Node.js is using the --inspect flag, which allows you to use Chrome's developer tools to debug. (With Chrome 55+, you can even debug both client and server side JavaScript code at the same time!)

sudo bin/node --inspect lib/main.js [args...]

You can additionally pass --debug-brk to pause on startup.

For more info:

Using the built-in CLI debugger

Node.js includes its own simple command-line debugger (not unlike R's browser()). You can start it like so:

sudo bin/node debug lib/main.js [args...]

For details: https://nodejs.org/api/debugger.html