Running and Debugging Node.js
On this page:
- Before you start
- Local and remote modes of running or debugging Node.js applications
- Running a Node.js application
- Debugging a Node.js application locally
- Debugging a running Node.js application
- Creating a Node.js run/debug configuration
- Enabling Live Editing during a Node.js debugging session
- Creating a Node.js remote debug configuration.
- Node.js multiprocess debugging
Before you start
Make sure the NodeJS plugin is enabled. The plugin is bundled with WebStorm and activated by default. If the plugin is not activated, enable it on the Plugins page of the Settings / Preferences Dialog as described in Enabling and Disabling Plugins.
Local and remote modes of running or debugging Node.js applications
Running a Node.js application in WebStorm is supported only in the local mode. This means that WebStorm itself starts the Node.js engine and the target application according to a run configuration and gets full control over the session.
Debugging can be performed in two modes:
- Locally, with the Node.js engine started from WebStorm.
- Remotely, when WebStorm connects to an already running Node.js application. This approach gives you the possibility to re-start a debugging session without re-starting the Node.js server.
You can also
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, WebStorm creates separate run configurations for the server-side and the client-side code,
but you specify all your settings in one dedicated NodeJS run configuration.
Running a Node.js application
- Create a Node.js run configuration.
-
To launch the application, select the run configuration from the list on the main tool bar
and then choose on the main menu or click the Run toolbar button
. The Run tool window opens.
-
Open the browser of your choice and open the page with the URL address generated through the
server.listen function
based on the
port
andhost
parameters. The page shows the result of executing your Node.js application.
Debugging a Node.js application locally
- Set the breakpoints in the Node.js code, where necessary. At least one breakpoint is necessary otherwise the program will be just executed. If you want the debugging tool to stop at the first line of your code, set a breakpoint at the first line.
- Create a Node.js run/debug configuration.
-
To start a debugging session,
select the required debug configuration from the list on the main tool bar
and then choose on the main menu or click the Debug toolbar button
.
- Open the browser of your choice and open the starting page of your application. Control over the debugging session returns to WebStorm.
- Switch to WebStorm, 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 running Node.js application
With WebStorm, you can connect to an already running Node.js applications. The application can be started either on the same machine or on a physically remote host.
When the application to debug is running on a physically remote host, you need to run a proxy or any other software that ensures port forwarding on the Node.js server.
This is necessary because the debug port can open only on the localhost
network interface. The localhost
network interface cannot be accessed from another machine
therefore WebStorm cannot connect to it upon initiating a debugging session.
-
Make sure the application to debug has been launched in the target environment with the following parameters:
--debug-brk=<port through which the debugger on the remote host interacts with the network interface which accepts external connections>
Please note the following:
-
With the
--debug-brk
option, the execution of the application suspends right after launch. This option allows you to debug the code executed on start. -
With the
--debug
option, the code that has to be executed on the application start is executed whereupon the application waits for a debugger to connect to it. This option is useful when you are not going to debug Node.js right now, but you want to debug it later.
-
With the
- Create a Node.js Remote Debug configuration: in the Debug port text box, type the port number through which you will interact with the remote host according to the server access configuration, see Creating a Remote Server Configuration.
-
With the application still running, launch the Node.js Remote Debug configuration (select the configuration in the list
and click the Debug toolbar button
).
- In the Run tool window, copy the URL address of the server and open the corresponding page in the browser. Control over the debugging session returns to WebStorm.
- Switch to WebStorm. 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.
Creating a Node.js run/debug configuration
- Choose on the main menu. Alternatively, click Shift+Alt+F10 and select Edit Configuration from the pop-up menu.
-
In the Edit Configuration dialog box that opens, click the Add New Configuration toolbar button
, and choose Node.js on the context menu.
-
In the Run/Debug Configuration: Node.js dialog box, that opens, specify the following:
- The name of the configuration.
-
In the Node Interpreter field, specify the Node.js installation to use.
Choose the local or remote interpreter from the drop-down list, or click
and choose the interpreter in the dialog box that opens, or configure an interpreter as described in Configuring Node.js Interpreters.
-
To enable remote debugging of the application, specify the following option in the Node parameters text box:
--debug-brk=<port for connect to debugger remotely>
-
With the
--debug-brk
option, the execution of the application suspends right after launch. This option allows you to debug the code executed on start. -
With the
--debug
option, the code that has to be executed on the application start is executed whereupon the application waits for a debugger to connect to it. This option is useful when you are not going to debug Node.js right now, but you want to debug it later.
-
With the
- In the JavaScript File field, specify the location of the file to start running the Node.js application from.
- If the file to run references any other files, specify their location in the Working directory field.
- If applicable, in the Application parameters text box, specify the arguments to be passed to the application on start through the process.argv array.
-
If necessary,
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, WebStorm creates separate run configurations for the server-side and the client-side code, but you specify all your settings in one dedicated NodeJS run configuration. - Click OK, when ready.
Enabling Live Editing during a Node.js debugging session
You can
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, WebStorm creates separate run configurations for the server-side and the client-side code,
but you specify all your settings in one dedicated NodeJS run configuration.
- Choose on the main menu. Alternatively, click Shift+Alt+F10 and select Edit Configuration from the pop-up menu.
- From the list, choose the Node.js run configuration to activate the Live Edit functionality in. In the dialog box that opens, switch to the Browser / Live Edit tab.
-
Select the After launch check box
to have a browser started automatically after a debugging session is launched. Specify the browser to use in the drop-down list next to the check box.
- To use the system default browser, choose Default.
- To use a custom browser, choose 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 box that opens. For more information, see Configuring Browsers.
- Select the With JavaScript debugger check box to enable the JavaScript debugger in the selected browser and specify the URL address to open the application at.
Creating a Node.js remote debug configuration.
- On the main menu, choose .
-
In the Edit Configuration dialog box, that opens, click the Add New Configuration toolbar button
, and choose Node.js Remote Debug on the context menu.
-
In the Run/Debug Configuration: Node.js Remote Debug dialog box, that opens, specify the following:
- The name of the configuration.
- The host where the target application is running.
- The port to connect to. Copy the port number from the information message in the Run tool window that controls the running application.
- Click OK, when ready.
Node.js multiprocess debugging
WebStorm 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.
-
Choose the newly created configuration in the Select run/debug configuration drop-down list on the tool bar
and click the Debug toolbar button
.
The Debug tool window opens and the Frames drop-down list shows the additional processes as threads as soon as they are launched:
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.