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.
Navigate to the above-created directory.
Run the Yeoman SharePoint Generator to create the solution.
Yeoman generator will present you with the wizard by asking questions about the solution to be created.
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.
In the command prompt, type the below command to open the solution in a code editor of your choice.
Get the Debugger Extension
In VS Code, click Extensions from left menu.
Then, search for debugger.
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.
- {
-
-
-
-
- "version": "0.2.0",
- "configurations": [{
- "name": "Local workbench",
- "type": "chrome",
- "request": "launch",
- "url": "https://localhost:4321/temp/workbench.html",
- "webRoot": "${workspaceRoot}",
- "sourceMaps": true,
- "sourceMapPathOverrides": {
- "webpack:///.././src/*": "${webRoot}/src/*",
- "webpack:///../../../src/*": "${webRoot}/src/*",
- "webpack:///../../../../src/*": "${webRoot}/src/*",
- "webpack:///../../../../../src/*": "${webRoot}/src/*"
- },
- "runtimeArgs": [
- "--remote-debugging-port=9222"
- ]
- },
- {
- "name": "Hosted workbench",
- "type": "chrome",
- "request": "launch",
- "url": "https://enter-your-SharePoint-site/_layouts/workbench.aspx",
- "webRoot": "${workspaceRoot}",
- "sourceMaps": true,
- "sourceMapPathOverrides": {
- "webpack:///.././src/*": "${webRoot}/src/*",
- "webpack:///../../../src/*": "${webRoot}/src/*",
- "webpack:///../../../../src/*": "${webRoot}/src/*",
- "webpack:///../../../../../src/*": "${webRoot}/src/*"
- },
- "runtimeArgs": [
- "--remote-debugging-port=9222",
- "-incognito"
- ]
- }
- ]
- }
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.
On the terminal window, type the below command.
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.
Debugging on Hosted Workbench
Follow the below steps to debug on real SharePoint site.
- From VS Code open launch.json under .vscode folder.
- In the hosted workbench configuration, type your SharePoint online site address for URL property.
- Optionally, remove -incognito from runtimeArgs.
- From debug menu, select Hosted workbench.
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.