Debug SharePoint Framework Solutions With Visual Studio Code

Overview

 
Debugging is an integral part of a software developer’s life. It helps to analyze the code scenarios during run time. In the previous article, we explored debugging SPFx solutions from the browser.
 
In this article, we will explore how we can debug the SPFx solutions with Visual Studio code.
 

Create SPFx Solution

 
Open the command prompt. Create a directory for SPFx solution.
  1. md spfx-debug-vscode  
Navigate to the above-created directory.
  1. cd spfx-debug-vscode  
Run the Yeoman SharePoint Generator to create the solution.
  1. yo @microsoft/sharepoint  
Yeoman generator will present you with the wizard by asking questions about the solution to be created.
 
Debug SharePoint Framework Solutions With Visual Studio Code 
 
Solution Name: Hit Enter to have a default name (spfx-debug-vscode in this case) or type in any other name for your solution.
Selected choice: Hit enter
 
Target for component: Here, we can select the target environment where we are planning to deploy the client web part, i.e., SharePoint Online or SharePoint OnPremise (SharePoint 2016 onwards).
Selected choice: SharePoint Online only (latest)
 
Place of files: We may choose to use the current folder or create a subfolder for our solution.
Selected choice: Use the current folder
 
Deployment option: Selecting Y will allow the app to be deployed instantly to all sites and will be accessible everywhere.
Selected choice: Y
 
Permissions to access web APIs: Choose if the components in the solution require permissions to access web APIs that are unique and not shared with other components in the tenant.
Selected choice: N (solution contains unique permissions)
 
Type of client-side component to create: We can choose to create a client-side web part or an extension. Choose the web part option.
Selected choice: WebPart
 
Web part name: Hit Enter to select the default name or type in any other name.
Selected choice: SPFxDebug
 
Web part description: Hit Enter to select the default description or type in any other value.
Selected choice: Debug SPFx with VS Code
 
Framework to use: Select any JavaScript framework to develop the component. Available choices are No JavaScript Framework, React, and Knockout.
Selected choice: No JavaScript Framework
 
Yeoman generator will perform scaffolding process to generate the solution. The scaffolding process will take a significant amount of time.
 
Once the scaffolding process is completed, lock down the version of project dependencies by running the below command.
  1. npm shrinkwrap  
In the command prompt, type the below command to open the solution in a code editor of your choice.
  1. code .  

Get the Debugger Extension

 
In VS Code, click Extensions from left menu.
 
Then, search for debugger.
 
Debug SharePoint Framework Solutions With Visual Studio Code 
 
Select and install “Debugger for Chrome” if not installed already.
 
Open launch.json under “.vscode” folder. This file contains configurations for local workbench and hosted workbench.
  1. {  
  2.   /** 
  3.    * Install Chrome Debugger Extension for Visual Studio Code to debug your components with the 
  4.    * Chrome browser: https://aka.ms/spfx-debugger-extensions 
  5.    */  
  6.   "version""0.2.0",  
  7.   "configurations": [{  
  8.       "name""Local workbench",  
  9.       "type""chrome",  
  10.       "request""launch",  
  11.       "url""https://localhost:4321/temp/workbench.html",  
  12.       "webRoot""${workspaceRoot}",  
  13.       "sourceMaps"true,  
  14.       "sourceMapPathOverrides": {  
  15.         "webpack:///.././src/*""${webRoot}/src/*",  
  16.         "webpack:///../../../src/*""${webRoot}/src/*",  
  17.         "webpack:///../../../../src/*""${webRoot}/src/*",  
  18.         "webpack:///../../../../../src/*""${webRoot}/src/*"  
  19.       },  
  20.       "runtimeArgs": [  
  21.         "--remote-debugging-port=9222"  
  22.       ]  
  23.     },  
  24.     {  
  25.       "name""Hosted workbench",  
  26.       "type""chrome",  
  27.       "request""launch",  
  28.       "url""https://enter-your-SharePoint-site/_layouts/workbench.aspx",  
  29.       "webRoot""${workspaceRoot}",  
  30.       "sourceMaps"true,  
  31.       "sourceMapPathOverrides": {  
  32.         "webpack:///.././src/*""${webRoot}/src/*",  
  33.         "webpack:///../../../src/*""${webRoot}/src/*",  
  34.         "webpack:///../../../../src/*""${webRoot}/src/*",  
  35.         "webpack:///../../../../../src/*""${webRoot}/src/*"  
  36.       },  
  37.       "runtimeArgs": [  
  38.         "--remote-debugging-port=9222",  
  39.         "-incognito"  
  40.       ]  
  41.     }  
  42.   ]  
  43. }  

Debugging on Local Workbench

 
Open the main web part file (src\webparts\spFxDebug\SpFxDebugWebPart.ts) and set a debug point to render method.
 
Click View > Terminal to open the terminal window.
 
Debug SharePoint Framework Solutions With Visual Studio Code 
 
On the terminal window, type the below command.
  1. gulp serve --nobrowser  
Now that we are ready to debug our code in VS Code, use any of the below options to start debugging.
  • On the keyboard, press F5.
  • From the main menu, click Debug > Start Debugging.
  • From left menu, click Debug and start debugging by clicking green arrow.
Debug SharePoint Framework Solutions With Visual Studio Code
 

Debugging on Hosted Workbench

 
Follow the below steps to debug on real SharePoint site.
  1. From VS Code open launch.json under .vscode folder.
  2. In the hosted workbench configuration, type your SharePoint online site address for URL property.
  3. Optionally, remove -incognito from runtimeArgs.

    Debug SharePoint Framework Solutions With Visual Studio Code

  4. From debug menu, select Hosted workbench.

    Debug SharePoint Framework Solutions With Visual Studio Code

Summary

 
SPFx solutions can be easily debugged in VS Code using debugger extensions. This gives comfort in developing, testing and debugging the SPFx solution from same IDE.