Running and Debugging Node.js
CLion helps you run and debug your Node.js applications. You can debug applications that are started from CLion as well as attach to already running applications.
Before you start
Install and enable the NodeJS plugin on the Plugins page as described in Installing plugins from repository.
Debugging a Node.js application
console.log() statements. You can do many things that will help you explore the code and understand where the bug is. In the Debug tool window, you can view the call stack and the variables in their current state, evaluate expressions in the editor, and step through the code.
You can initiate a debugging session in two ways:
Start the debugger together with your application using a Node.js run/debug configuration.
Attach the debugger to an already running application. In this case, your application is already running in the debug mode and CLion attaches to a running process.
--inspect-brk, and now deprecated
--debugflags so you can make any application accessible for debugging.
To debug a running application, use an Attach to Node.js/Chrome configuration.
Starting the debugger together with a Node.js application on your computer
Set the breakpoints in the code where necessary.
Select the newly created Node.js configuration from the Select run/debug configuration list on the toolbar and click next to it. The Debug Tool Window opens.
Perform the steps that will trigger the execution of the code with the breakpoints.
Switch to CLion, where the controls of the Debug tool window are now enabled. Proceed with the debugging session — step through the breakpoints, switch between frames, change values on-the-fly, examine a suspended program, evaluate expressions, and set watches.
Debugging a Node.js application that uses nodemon
The CLion built-in debugger can automatically reconnect to running Node.js processes. This lets you debug Node.js applications that use the nodemon utility, which automatically reloads your Node.js process when the code is updated.
To debug such application, you need to start it in the debug mode (with the
--inspect-brk flag) and then connect to it using the Attach to a Node.js/Chrome debug configuration with the Reconnect Automatically option on.
To install nodemon
To start an application with nodemon in the debug mode
Create and run the following
npm debugscript:debug": "nodemon --inspect <path_to_the_file_that_starts_your_application>
See Running and debugging scripts for details.
To debug an application
Set the breakpoints in your code as necessary.
Create a new Attach to a Node.js/Chrome configuration as described in Debugging a running Node.js application and select the Reconnect automatically checkbox.
Usually, you don’t need to change the port in the configuration
9229because it’s the default port the debugger is listening on. But you can double-check what port is used in the message logged when you run the app in the debug mode.
Select the newly created Attach to Node.js/Chrome configuration from the Select run/debug configuration list on the toolbar and click next to it. The debugger stops at the breakpoints that you put in your code in CLion.
Now, every time you make any changes to the code and save them Ctrl+S, nodemon will automatically reload the application and the debugger will automatically re-attach to the restarted process.