Skip to content
This repository has been archived by the owner on May 15, 2019. It is now read-only.

Debugging with Visual Studio Code

Jason Dobry edited this page Mar 8, 2017 · 5 revisions

Table of Contents

The Visual Studio Code Debugger

One of the key features of Visual Studio Code is its great debugging support. VS Code's built-in debugger helps accelerate your edit, compile and debug loop.

Debugging functions

Each deployed function has its own worker process, separate from the main Emulator process. This is more secure and safer for the Emulator and simulates production, but it means there's a different debugger running for each deployed function.

Standard Node.js Debugger

For more information on the standard Node.js Debugger, see Node.js Debugger.

  1. Open Visual Studio Code in your project folder.

  2. Create a new launch configuration (in the launch.json file) for your project:

    {
      "version": "0.2.0",
      "configurations": [
        {
          "name": "Debug Function",
          "type": "node",
          "request": "attach",
          "port": 5858
        }
      ]
    }
    

    Be sure to set type to node.

  3. Open the file that you want to debug and set a breakpoint by clicking to the left of the line number of the line where you want to set the breakpoint.

  4. Deploy the function you want to debug, e.g.:

    functions deploy helloWorld --trigger-http
    

    See Deploying functions.

  5. To debug a function using the standard Node.js debugger, run the following:

    functions debug helloWorld
    

    You should see something like the following printed to the console:

    Debugger for helloWorld listening on port 5858.
    

    You can also configure the Debugger's port and/or force the function to pause execution until you attach to the Debugger. Run functions debug --help for details.

  6. In Visual Studio Code, go to the Debug View and select your "Debug Function" launch configuration.

  7. Click the green arrow button to attach to the debugger and start debugging.

  8. Call your function to start debugging it, seen below:

    VSCode Debugging 3

  9. When you're done, click the red "Disconnect" button to disconnect from the debugger.

  10. To take your function out of debug mode, run the following:

    functions reset helloWorld
    

    or to restart the function and keep its debugging settings, run the following:

    functions reset helloWorld --keep
    

    Run functions debug --help for details.

V8 Inspector Integration

For more information on the new Node.js V8 Inspector integration, see Node.js V8 Inspector integration. See also Debugging with Chrome DevTools.

  1. Open Visual Studio Code in your project folder.

  2. Create a new launch configuration (in the launch.json file) for your project:

     {
       "version": "0.2.0",
       "configurations": [
         {
           "name": "Inspect Function",
           "type": "node2",
           "request": "attach",
           "port": 9229
         }
       ]
     }
    

    Be sure to set type to node2.

  3. Open the file that you want to debug and set a breakpoint by clicking to the left of the line number of the line where you want to set the breakpoint.

  4. Deploy the function you want to debug, e.g.:

    functions deploy helloWorld --trigger-http
    

    See Deploying functions.

  5. To inspect a function using the new Node.js V8 Inspector integration, run the following:

    functions inspect helloWorld
    

    You should see something like the following printed to the console:

    Debugger for helloWorld listening on port 9229.
    

    You can also configure the Debugger's port and/or force the function to pause execution until you attach to the Debugger. Run functions inspect --help for details.

  6. In Visual Studio Code, go to the Debug View and select your "Inspect Function" launch configuration.

  7. Click the green arrow button to attach to the debugger and start debugging.

  8. Call your function to start debugging it, seen below:

    VSCode Debugging 3

  9. When you're done, click the red "Disconnect" button to disconnect from the debugger.

  10. To take your function out of debug mode, run the following:

    functions reset helloWorld
    

    or to restart the function and keep its debugging settings, run the following:

    functions reset helloWorld --keep
    

    Run functions reset --help for details.

Additional Visual Studio Code reading