Running and debugging Node.js application

Let’s continue our series of posts and tutorials on Node.js development in WebStorm.

Our first post covered the basic WebStorm configuration for working with Node.js projects. Now let’s move on to running and debugging Node.js apps both locally and remotely.

Running Node.js app locally

WebStorm allows you to run Node.js application locally on your machine: you should create a Node.js Run/Debug configuration for the file you need to execute and click Run.

To create a new Run/Debug configuration, click Edit configurations in the top right corner of the IDE window, or in the main menu Run.

nodejs-edit-configurations

Click Add new configuration and seleсt Node.js type.

In this new Run/Debug configuration, specify the path to the JavaScript file that needs to be executed. For example, for a simple Express web app it should be a file that configures and starts the web server.

nodejs-run-configuration

You can also add any parameters you need to pass to node (Node parameters input) or to your application (Application parameters input) on the start.

Update: Starting with Express 4.0 file www in bin folder needs to be executed to start the app. When creating a new Express web application in WebStorm 8.0.2 or higher, the required Run/Debug configuration will be created automatically.

Select this new configuration and click Run. The Run tool window with console will appear showing the app execution log.

nodejs-run

Debugging Node.js app locally

The same configuration can be used to debug your Node.js code.

The built-in Node.js debugger allows you to step through the breakpoints, change values on the fly, evaluate expressions, and set watches.

Click the Debug button for the created Run/Debug configuration in the top right-hand corner of the IDE, from the Run menu or with the shortcut (Shift+F9 on Windows or Linux, Ctrl+D on Mac). A tool window with variables view, frames and watchers for your debug session will appear:

nodejs-debug-session

Debugging Node.js app that runs remotely

With Remote Node.js Debug configuration you can debug Node.js apps that are running remotely. You’ll be able to restart the debug session without restarting the app itself.

Select Remote Node.js Debug type when creating a new debug configuration, then specify the host you need to connect to and the port.

nodejs-remote-configuration

Now that the debug configuration is ready, select it from the drop-down list of configurations on the right and click Debug.

You’ll see the Debug tool window when the breakpoint is reached. You’ll be able to use full-featured debugger in the same way as if you were debugging your code locally.

Please note that for remote debug your app should be running with –debug=<port for debugging> or –debug-brk=<port for debugging> parameter.

In Node.js you can’t access the port for debugging externally, which is why you need to configure a proxy or any other software that ensures port forwarding on the Node.js server (for an example of how to do that see stackoverflow).

Have a look at the previous post on Node.js development in our blog: “Getting started with Node.js in WebStorm”.

Develop with pleasure!
-JetBrains WebStorm Team

About Ekaterina Prigara

Ekaterina Prigara is WebStorm product marketing manager at JetBrains. She's passionate about new technologies, UX and coffee.
This entry was posted in Tutorials and tagged . Bookmark the permalink.

26 Responses to Running and debugging Node.js application

  1. Chris says:

    It would be nice to cover simultaneously debugging javascript in the browser when running a Node.js app.

    It took me a while to discover I had to edit the JavaScript Debug target’s “Remote URLs of local files” and specify the full URL of the static javascript directory, to get WS7 to honor breakpoints when connected to Firefox.

  2. Pingback: [Webinar Recording] Node.js Development Workflow | JetBrains WebStorm Blog

  3. Pingback: Webinar Recording: Node.js Development Workflow in WebStorm | JetBrains Company Blog

  4. angel says:

    sorry, I follow the steps but I can’t find than the debug stop in my breakpoint, I set the breakpoints, then configure the debugger to point my file and directory, run it and in the console shows than a port is open…what is the next step??…there is some tutorial or video showing how debug??…thanks!

  5. Pingback: Getting Started with Node.js in WebStorm | JetBrains WebStorm Blog

  6. Pingback: Guide to Node.js Development with WebStorm | JetBrains WebStorm Blog

  7. Eric Swann says:

    This demo may no longer be accurate. On windows (x64), I followed the directions and simply got: “Process finished with exit code 0″. No server startup.
    However, the template also comes with a default configuration “bin\www” which did work. It seems that instead of selecting “app.js” for the javascript file to run, you must specify “bin\www” when setting up the config. The demo also doesn’t specify how to set up the default port, which I had to manually add in the app.js: app.set(‘port’, process.env.port || 3000). I see it in the animated image, but there’s no explanation and it’s actually after the lines that state to run the project.

    • Ekaterina Prigara says:

      bin/www configuration was added just recently with WebStorm 8 bug fix update after Express 4 release where the seed project structure was changed. I would update the tutorial.

      • Eric Swann says:

        Cool thanks for the explanation!

      • Maheshwari says:

        I have downloaded evaluation version. And I also get the same “Process finished with exit code 0″ error. I tried to set http://www.js but I am not able to find any such file or setting. Can you please elaborate on this. Probably add the gif file for this update, same as node-rdc.gif?

        Thanks,
        Maheshwari.

        • Ekaterina Prigara says:

          Hello Maheshwari,
          You’ve created a new Express project with Create new project dialog, right? Then you should have a predefined run/debug configuration called www/bin that starts the app.

  8. Lola says:

    Hi,
    I have a problem in WebStorm’s debug Tool with the variables panel. When i am debugging a Node application the “Set value” option is inactive.
    What is the problem?

    Thanks,

  9. Bryan Rayner says:

    This is great – I’m really looking for help in understanding why some parts of the execution flow can’t be stepped into, or stepped over? It seems to be a problem with how I’ve configured WebStorm, but it’s configured exactly as this blog post describes. How do you get the step into / step over to work properly on a Node.js app?

    • Ekaterina Prigara says:

      You mean you get different behaviour when stepping in/over from the video?

      • Ivan Zhukov says:

        He means that when evaluating stops on a breakpoint, pushing Step Over, Step Into doesn’t continue debugging, it just resuming script execution… The same happens for me in WebStorm 8.0.4 and Node v0.10.32.
        Is that even possible to step over/in when debug server JS?

  10. H Lock says:

    Hi,
    I am debugging in WS 10.0.2 on node.js while developping with http server (under the hood of jayData odata-server).
    I run the server (with breakpoints set in a data provider) in debug mode under WS and the send a request (via Browser or nodejs http.request doesn’t matter).
    WS reaches the breakpoint, shortly pauses and then aborts.
    Console output:
    Process finished with exit code -1073741510 (0xC000013A: interrupted by Ctrl+C)

    Could somebody please help me?

  11. Maxim Boltenkov says:

    Hi!
    Try to clear your ‘Watches’! For me it’s work.

  12. mangesh says:

    Thanks for demo…work for me very well …follow steps open browser and type url localhost:3000 ..showing me Express Welcome to Express..Thank you so much..Thanks

  13. susnick says:

    This does not work for me. I can run the configuration just fine, but debug the configuration does not work. From what i can see is that Run calls “node.exe bin\www.js”, while Debug thinks its debugging remotely i guess. It runs “node.exe –debug-brk=62506 –nolazy bin\www.js”. I have not been able to locate where these parameters are being set to remove them. Can you help?

    • Ekaterina Prigara says:

      All seems correct. That’s a proper parameters for debug configuration, not a remote one. Why do you think that debug doesn’t work? Can you run your app from WebStorm? Does it stop on breakpoints when you start a debug session?

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">