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.
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.
Local and Remote debugging
CLion supports two debugging modes:
Local debugging: in this mode, your application is started from CLion and is running locally on your computer. To debug it, use a Node.js configuration.
Debugging a remote application: in this mode, your application is running in a remote environment in the debug mode and CLion attaches to a running process. CLion recognizes
--inspectand now deprecated
--debugflags so you can make any application accessible for remote debugging. To debug a remote application, use an Attach to Node.js/Chrome configuration.
Debugging a Node.js application locally
Set the breakpoints in the Node.js 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.
Using interactive Debugger Console
When you are debugging a Node.js application, CLion shows two console tabs in the Debug tool window - Console and Debugger Console.
Start typing a statement at
>in the input field. As you type, CLion suggests variants for completion.
Select the relevant statement and press Enter. CLion shows its value in the console.
Navigate to source code
At each line with output of
console.*, CLion shows the name of the file and the line where it was called. Click this link to jump to the call in the source code.
The Debugger Console also shows stack traces. Click the link next to a reported problem to jump to the line of code where this problem occurred.
Filter out messages
The console shows objects in a tree view, with stack traces collapsed by default. Warnings (
console.warn()), errors (
console.error()), and info (
console.info()) messages have different icons and background colors to make them easier to notice.
To hide log messages of specific types, click and select the severities to filter out.
The log messages grouped using
console.groupEnd()are displayed as a tree. To show the output collapsed by default, use
Apply CSS styles
Use CSS and the
%cmarker to apply styles to log messages.
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 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
In the embedded Terminal (Alt+F12), type
npm install --save-dev nodemonor
yarn add nodemon --devto install nodemon as a development dependency.
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>
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 (
9229) because 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.
Node.js multiprocess debugging
CLion supports debugging additional Node.js processes that are launched by the child_process.fork() method or by the cluster module. Such processes are shown as threads in the Frame pane on the Debugger tab of the Debug Tool Window.
Set the breakpoints in the processes to debug.
Create a Node.js run/debug configuration.
Select the newly created configuration from the Select run/debug configuration list on the toolbar and click Debug .
The Debug Tool Window opens and the Frames list shows the additional processes as threads as soon as they are launched:
and so on To examine the data (variables, watches, and so on) for a process, select its thread in the list and view its data in the Variables and Watches panes. When you select another process, the contents of the panes are updated accordingly.