GoLand 2019.1 Help

Running and Debugging Node.js

GoLand helps you run and debug your Node.js applications. You can debug applications that are started from GoLand as well as attach to already running applications.

Before you start

Install the NodeJS plugin on the Plugins page as described in Managing plugins.

Running a Node.js application

GoLand runs Node.js applications according to a run configuration of the type Node.js. GoLand also uses this configuration for debugging Node.js applications locally.

To create a Node.js run/debug configuration

  1. From the main menu, choose Run | Edit Configuration, then in the Edit Configurations dialog, click the Add button on the toolbar and select Node.js from the list. The Run/Debug Configuration: Node.js dialog opens.

  2. Specify the Node.js interpreter to use. This can be a local Node.js interpreter or a Node.js on Windows Subsystem for Linux.

  3. In the JavaScript File field, specify the path to the main file of the application that starts it (for example, bin/www for Node.js Express applications).

  4. Optionally:
    • Specify the Node Parameters that customize the start of Node.js. For example, to make your application accessible for remote debugging, type one of the debugging flags depending on your Node.js version: --inspect=<debugger port> for Node.js versions 6.5 and higher or --debug=<debugger port> for any Node.js version earlier than 8. The default debugger port is 5858.

    • In the Application parameters field, specify the Node.js-specific arguments to be passed to the application on start through the process.argv array.

To run an application

  • Select the newly created Node.js configuration from the Select run/debug configuration list on the toolbar and click Run next to it. The application starts, and the Run Tool Window opens showing the application output.

If you are using a logging tool like morgan in your application and this tool writes logs to a file, you can see these logs in the Console tab of the Run tool window.

To manage logs when running a Node.js application

  1. Create a Node.js run/debug configuration as described above and go to the Logs tab.

  2. Click the Add button next to the Log files to be shown in console field which lists the available log files (if any).

  3. In the Edit Log Files Aliases dialog that opens, type the alias name to show in the list of log entries and specify the location of the log file. Select whether you want to show all files that this pattern covers or only the last one.

  4. Click OK to return to Node.js Run/Debug Configuration dialog, where the new log file is added to the list. Select the Is Active checkbox next to it. To skip the previous content, select the Skip Content checkbox.

  5. Optionally:
    • To enable saving the console output to a log file, select the Save console output to file checkbox and specify the file location.

    • Choose when you want the Console shown.

    Node.js run/debug configuration: Log tab

Debugging a Node.js application

GoLand 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

GoLand supports two debugging modes:

  • Local debugging: in this mode, your application is started from GoLand 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 GoLand attaches to a running process. GoLand recognizes --inspect and now deprecated --debug flags 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

  1. Set the breakpoints in the Node.js code where necessary.

  2. Create a Node.js run/debug configuration as described above. If necessary, GoLand can generate a JavaScript Debug configuration and start it automatically together with the Node.js configuration as described in Starting a JavaScript Debug configuration together with a Node.js configuration.

  3. Select the newly created Node.js configuration from the Select run/debug configuration list on the toolbar and click the Debug button next to it. The Debug Tool Window opens.

  4. Perform the steps that will trigger the execution of the code with the breakpoints.

  5. Switch to GoLand, 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, GoLand shows two console tabs in the Debug tool window - Console and Debugger Console.

  • The Console tab shows the output of the node process itself, that is, everything that is written to process.stdout and process.stderr directly or is logged using console.*.

    Node.js debugging: Console tab
  • In the Debugger Console, you can run JavaScript code snippets and view the console.* messages.

Run JavaScript in the Debugger Console

  1. Start typing a statement at > in the input field. As you type, GoLand suggests variants for completion.

  2. Select the relevant statement and press Enter. GoLand shows its value in the console.

Navigate to source code

  • At each line with output of console.*, GoLand 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 the Filter button and select the severities to filter out.

    Node.js interactive debugger console: filtering out messages by type

Group messages

  • The log messages grouped using console.group() and console.groupEnd() are displayed as a tree. To show the output collapsed by default, use console.groupCollapsed().

    Node.js interactive debugger console: log messages grouped

Apply CSS styles

  • Use CSS and the %c marker to apply styles to log messages.

    Node.js interactive debugger console: applying CSS style to log messages

Debugging a running Node.js application

With GoLand, you can debug an application that is running in a remote environment. GoLand supports remote debugging with the Chrome Debugging Protocol and the V8 Debugging Protocol (also known as Legacy Protocol). In either case, a debugging session is initiated through an Attach to Node.js/Chrome configuration.

To debug with Chrome Debugging Protocol

  1. Set the breakpoints as necessary.

  2. Choose Run | Edit Configurations from the main menu, then click Add New Configuration in the Edit Configuration dialog that opens, and choose Attach to Node.js/Chrome from the list. The Run/Debug Configuration: Attach to Node.js/Chrome dialog opens.

  3. Specify the host where the target application is running and the port passed to --inspect when starting the Node.js process to connect to. Copy the port number from the information message Debugger listening <host>:<port> in the Terminal tool window or in the Run tool window that controls the running application. The default port is 9229.

  4. In the Attach to area, choose Chrome or Node.js > 6.3 started with --inspect.

  5. Select the newly created Attach to Node.js/Chrome configuration from the Select run/debug configuration list on the toolbar and click the Debug button next to it. The Debug Tool Window opens.

  6. Perform the actions that will trigger the code at the breakpoint. Control over the debugging session returns to GoLand.

  7. Switch to GoLand. In the Debug tool window, step through the breakpoints, switch between frames, change values on-the-fly, examine a suspended program, evaluate expressions, and set watches.

To debug with V8 Debugging Protocol

  1. Create an Attach to Node.js/Chrome run/debug configuration as described above and specify the host and the port passed to --debug. The default port is 9229.

  2. Make sure the application to debug has been launched in the remote environment with the following parameters: --debug=<debugger port>. The default port is 5858.

  3. Proceed as during a debugging session with Chrome Debugging Protocol.

Debugging a Node.js application that uses nodemon

The GoLand 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 nodemon or yarn add nodemon --dev to install nodemon as a development dependency.

To start an application with nodemon in the debug mode

  • Create and run the following npm debug script:

    debug": "nodemon --inspect <path_to_the_file_that_starts_your_application>

  • Alternatively, pass the inspect flag through a Node.js run/debug configuration as described above.

To debug an application

  1. Set the breakpoints in your code as necessary.

  2. 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.

    Attach no Node.js run configuration: 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.
    Node.js application with  nodemon running in the debug mode: check the port

  3. Select the newly created Attach to Node.js/Chrome configuration from the Select run/debug configuration list on the toolbar and click Debug button next to it. The debugger stops at the breakpoints that you put in your code in GoLand.

    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.

Starting a JavaScript Debug configuration together with a Node.js configuration

With GoLand, you can debug the server-side code and the client-side JavaScript code of your application in two modes:

  • Separately, using single-run configurations.

  • Simultaneously, using a complex Node.js with JavaScript Debug configuration. All you need is configure the behaviour of the browser and enable debugging the client-side code of the application. This functionality is provided through a JavaScript Debug run configuration, so technically, GoLand 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.

To create a compound Node.js/JavaScript Debug configuration

  1. On the main menu, select Run | Edit Configuration.

  2. From the list, select the Node.js run configuration to start together with a JavaScript Debug configuration. In the dialog that opens, switch to the Browser / Live Edit tab.

  3. Select After launch to start a browser automatically when you launch a debugging session.

  4. In the field below, type the URL address to open the application at.

  5. Choose the browser to use from the list next to the After launch checkbox.
    • To use the system default browser, select Default.

    • To use a custom browser, select it from the list. Note that Live Edit is fully supported only in Chrome.

    • To configure browsers, click the Browse button and adjust the settings in the Web Browsers dialog that opens. For more information, see Configuring Browsers.

  6. Select With JavaScript debugger.

As for any other JavaScript debugging session, you can enable the Live Edit functionality as described in Live_Editing.

To enable Live Edit in a Node.js application

  1. Install the LiveEdit plugin on the Plugins page as described in Managing plugins.

  2. In the Settings/Preferences dialog (Ctrl+Alt+S), click Debugger under Build, Execution, Deployment, and then click Live Edit. The Live Edit page opens.

  3. Select Update Node.js application on changes. Specify the time-delay between changing the code in the editor and showing this change in the browser: accept the default value 300 ms or specify a custom value using the spin box next to the corresponding field.

Node.js multiprocess debugging

GoLand 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. Select the newly created configuration from the Select run/debug configuration list on the toolbar and click Debug the Debug button.

    The Debug Tool Window opens and the Frames list shows the additional processes as threads as soon as they are launched:

    ws_node_multiprocess.png

    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.

Last modified: 17 May 2019