CLion 2017.3 Help

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. CLion also recognizes --inspect and --debug flags so you can make any application accessible for remote debugging.

Before you start

Install and enable the NodeJS plugin. The plugin is not bundled with CLion, but it can be installed from the JetBrains plugin repository as described in Installing, Updating and Uninstalling Repository Plugins and Enabling and Disabling Plugins.

Debugging a Node.js application

CLion makes it easier to debug Node.js applications. You can put breakpoints right in your JavaScript or TypeScript code so you no longer need any debugger and 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. CLion supports remote debugging with the Chrome Debugging Protocol the V8 Debugging Protocol (also known as Legacy Protocol). In either case, a debugging session is initiated through a run/debug configuration of the type Attach to Node.js/Chrome.

Debugging a Node.js application locally

  1. Set the breakpoints in the Node.js code where necessary. If you want the debugging tool to stop at the first line of your code, set a breakpoint at the first line.
  2. Create a Node.js run/debug configuration as described above. If necessary, configure the behaviour of the browser and enable debugging the client-side code of the application. To do that, open the Browser / LiveEdit tab, choose the browser to open, and select the With JavaScript Debugger checkbox. This functionality is provided through a JavaScript Debug run configuration, so technically, CLion creates separate run configurations for the server-side and the client-side code, but you specify all your settings in one dedicated Node.js run configuration.
  3. Choose the newly created Node.js configuration in the Select run/debug configuration drop-down list on the toolbar and click /help/img/idea/2017.3/debug.png next to it. The Debug Tool Window opens.
  4. In the browser of your choice, open the starting page of your application. Control over the debugging session returns to CLion.
  5. 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 in a Docker container

CLion supports debugging of Node.js applications in Docker containers through run/debug configurations of the type Node.js. To debug an application on Docker, you need to choose a remote Node.js interpreter on Docker and configure folder and port bindings in the run/debug configuration.

To choose the Node.js interpreter on Docker

  1. Create a Node.js run/debug configuration as described above.
  2. Select one of the configured Node.js interpreters from the drop-down list or configure a new one as described in Configuring Node.js Interpreters.

To specify the Docker container settings
Click /help/img/idea/2017.3/browseButton.png next to the Edit Docker Container Settings field and specify the settings in the Edit Docker Container Settings dialog that opens.
Alternatively, select the Auto configure checkbox to do it automatically:

  • CLion creates a new image and installs the npm modules in it.
  • CLion runs the container with the new image, binds your project folder to /opt/project folder in the container to ensure synchronization on update, and maps /opt/project/node_modules to the OS temporary directory.
For details, see Working with volume bindings.

To configure port bindings

  1. Click /help/img/idea/2017.3/browseButton.png next to the Docker Container Settings field.
  2. In the Edit Docker Container Settings dialog that opens, expand the Port bindings area.
  3. Click /help/img/idea/2017.3/new.png and in the Port bindings dialog thaat opens, map the ports as follows:
    • In the Container port text box, type the port specified in your application.
    • In the Host port text box, type the port through which you want to open the application in the browser from your computer.
    • In the Host IP text box, type the IP address of the Docker's host, the default IP is 192.168.99.100. The host is specified in the API URL field on the Docker page of the Settings / Preferences Dialog.
    • Click OK to return to the Edit Docker Container Settings dialog where the new port mapping is added to the list.
  4. Click OK to return to the Run/Debug Configuration: Node.js dialog.

To start debugging

  1. Set the breakpoints in the Node.js code as necessary.
  2. Choose the newly created Node.js configuration in the Select run/debug configuration drop-down list on the toolbar and click /help/img/idea/2017.3/debug.png next to it.
  3. Proceed as during a local debugging session, as described above.

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.

  1. Set the breakpoints in the processes to debug.
  2. Create a Node.js run/debug configuration.
  3. Choose the newly created configuration in the Select run/debug configuration drop-down list on the tool bar and click the Debug toolbar button /help/img/idea/2017.3/debug.png.

    The Debug tool window opens and the Frames drop-down list shows the additional processes as threads as soon as they are launched:

    ws_node_multiprocess.png

    To examine the data (variables, watches, etc.) for a process, select its thread in the list and view its data in the Variables and Debug Tool Window. Watches panes. When you select another process, the contents of the panes are updated accordingly.

Last modified: 24 October 2017

See Also