CLion 2017.1 Help

Debugging JavaScript in Chrome

Before you start, configure the JavaScript debugger and JetBrains Chrome extension.

On this page:

Debugging an application running on the built-in server

CLion has a built-in web server that can be used to preview and debug your application. This server is always running and does not require any manual configuration. All the project files are served on the built-in server with the root URL http://localhost:<built-in server port>/<project root>, with respect to the project structure.

  1. Set the breakpoints in the JavaScript code, as required.
  2. Open the HTML file that references the JavaScript to debug or select the HTML file in the Project view.
  3. On the context menu of the editor or the selection, choose Debug <HTML_file_name>. The automatically generated debug configuration starts a debugging session. The file opens in the default browser, and the Debug tool window appears.

    You can also have the automatically generated configuration saved for further re-use. To do that, choose Create <HTML_file_name> on the context menu and then click OK in the dialog box that opens.

  4. If the JetBrains Chrome extension is not installed and activated in your browser, the Debug tool window shows a message with a link to the web store where the extension is available. Click the link and install the extension.
  5. In the Debug tool window, proceed as usual: step through the program, stop and resume program execution, examine it when suspended, view actual HTML DOM, edit the code, etc.

Example

Suppose you have a simple application that consists of an index.html file and a MyJavaScript.js file, where index.html references MyJavaScript.js. To start debugging this application using the built-in server, open index.html in the editor and choose Debug 'index.html'on the context menu:

ws_quick_start_debug_built_in_server_1.png
CLion creates a run/debug configuration automatically, and a debugging session starts:
ws_quick_start_debug_built_in_server_2.png
To restart the new run/debug configuration, click /help/img/idea/2017.1/debug.png in the upper right-hand corner of the CLion window or choose Run | Debug on the main menu:
ws_quick_start_debug_built_in_server_3.png

Debugging server-side JavaScript running on an external web server

Often you may want to debug server-side JavaScript running on an external development web server, e.g. powered by Node.js.

  1. Set the breakpoints in the JavaScript code, as required.
  2. Run npm start to launch the application in the development mode. You can do this either in the Terminal tool window or by double-clicking the task in the npm tool window, see Running NPM Scripts. When the development server is ready, switch to the Console tab and copy the URL address at which the application is running. You will need to specify this URL address in the run/debug configuration.
  3. Create a permanent debug configuration of the type JavaScript Debug:
    Choose Run | Edit Configuration on the main menu, click the Add New Configuration button add.png on the toolbar, and select JavaScript Debug from the pop-up list. In the Run/Debug Configuration: JavaScript Debug dialog box that opens, сhoose the browser to debug the application in (Chrome or another browser of the Chrome family) and specify the URL address at which the application is running. This URL can be copied in the Console tab of the Run tool window. Click OK to save the configuration settings.
  4. 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.1/debug.png. The HTML file specified in the run configuration opens in the chosen browser and the Debug tool window appears.
  5. If the JetBrains Chrome extension is not installed and activated in your browser, the Debug tool window shows a message with a link to the web store where the extension is available. Click the link and install the extension.
  6. In the Debug tool window, proceed as usual: step through the program, stop and resume program execution, examine it when suspended, view actual HTML DOM, edit the code, etc.

See Debugging React Applications and Debugging Angular Applications for examples.

Debugging asynchronous code

CLion supports debugging asynchronous client-side JavaScript code, currently this functionality is available only in Chrome. The asynchronous debugging mode is toggled through the Async check box on the tool bar of the Debugger tab in the Debug tool window. The check box is displayed only during a JavaScript debugging session.

  • When this check box is selected, CLion recognizes breakpoints inside asynchronous code and stops at them and lets you step into asynchronous code. As soon as a breakpoint inside an asynchronous function is hit or you step into asynchronous code, a new element Async call from <caller> is added in the Frames pane of the Debugger tab. CLion displays a full call stack, including the caller and the entire way to the beginning of the asynchronous actions.
  • When the check box is cleared, CLion does not recognize and therefore skips breakpoints in the asynchronous code and does not allow you to step into it.
The image below shows an example of a JavaScript debugging session.
ws_debug_tool_window_async.png
  • With the Async check box selected, the debugger will stop at line3(breakpont), then at line5(breakpoint). On clicking Step into, the debugger will stop at line5 (on function), then will move to line6.
  • With the Async check box cleared, the debugger will stop at line3(breakpont), then at line5(breakpoint). On clicking Step into, the debugger will move to line9.

Debugging workers

CLion supports debugging Service Workers and Web Workers. CLion recognizes breakpoints in each worker and shows the debug data for it as a separate thread in the Frame pane on the Debugger tab of the Debug Tool Window.

Note that CLion can debug only dedicated workers, debugging for shared workers is currently no supported.

  1. Set the breakpoints in the Workers to debug.
  2. If you are using Service Workers, make sure the Allow unsigned requests check box on the Debugger page is selected. Otherwise your service workers may be unavailable during a debug session:
    ws_debug_service_workers.png
  3. Create a permanent debug configuration of the type JavaScript Debug as described above in Starting a debugging session through a user-defined configuration.
  4. 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.1/debug.png.

    The HTML file specified in the run configuration opens in the chosen browser and the Debug tool window opens with the Frames drop-down list showing all the Workers:

    ws_js_debug_workers_frames.png

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

See Also

Last modified: 29 March 2017