Setting Up Local Debugging for SPFx Development

Introduction

Now that you’ve set up your SharePoint Framework (SPFx) development environment and created your first web part, the next step is learning how to debug and develop continuously in your local environment. Debugging is essential to ensure your web part works as expected before deploying it to SharePoint. This article will cover step-by-step instructions to set up local debugging and explain how to make iterative changes to your project.

1. Understanding the SPFx Development Process

When developing SPFx web parts, the local workbench provides an isolated environment where you can test your web parts. However, to fully interact with SharePoint services (like Lists or Libraries), you’ll often need to use the SharePoint Online workbench. Both environments support live updates through Gulp tasks, which means changes in your code can be reflected immediately in the browser without having to redeploy.

SPFx Development Workflow:

  1. Code your solution.
  2. Run it locally using Gulp.
  3. Test it in the workbench.
  4. Debug and fix any issues.
  5. Deploy it to SharePoint when ready.

2. Running Gulp for Continuous Development

Step 1: Start the Gulp Development Server

Once you’ve scaffolded your SPFx project and installed dependencies, you can begin development using Gulp.

  1. Open your command prompt or terminal.
  2. Navigate to your project directory:
   cd my-spfx-webpart
  1. Run the Gulp command to start the development server:
   gulp serve

Step 2: Access the Local Workbench

  1. Open your web browser.
  2. Navigate to the local workbench URL:
   https://localhost:4321/temp/workbench.html

This URL will load the local workbench, where you can add your web part and see changes in real time.

3. Configuring Debugging with Browser Developer Tools

Using Chrome Developer Tools

Debugging your web part can be efficiently done using browser developer tools, particularly in Chrome. Here’s how to use them:

  1. Open Chrome Developer Tools:
  • Right-click on the web page and select Inspect or press Ctrl + Shift + I (Windows/Linux) or Cmd + Option + I (Mac).
  1. Set Breakpoints:
  • Navigate to the Sources tab.
  • Find your web part’s code under the file tree (it will typically be located in the dist folder).
  • Click on the line number where you want to set a breakpoint.
  1. Debug Your Code:
  • Trigger the code that reaches your breakpoint by interacting with your web part.
  • The execution will pause at your breakpoint, allowing you to inspect variables and step through your code.

Using Visual Studio Code for Debugging

If you prefer using Visual Studio Code (VS Code) for debugging, follow these steps:

  1. Open your project in Visual Studio Code.
  2. Install the Debugger for Chrome extension (if you haven’t already):
  • Go to the Extensions view by clicking on the Extensions icon in the Activity Bar on the side.
  • Search for Debugger for Chrome and install it.
  1. Create a Debug Configuration:
  • Click on the Run and Debug icon in the Activity Bar.
  • Click on create a launch.json file.
  • Select Chrome from the options. Your launch.json should look similar to this:
   {
       "version": "0.2.0",
       "configurations": [
           {
               "type": "chrome",
               "request": "launch",
               "name": "Launch Chrome",
               "url": "https://localhost:4321/temp/workbench.html",
               "webRoot": "${workspaceFolder}"
           }
       ]
   }
  1. Start Debugging:
  • Set breakpoints in your code in VS Code.
  • Press F5 or click on the green play button in the Run and Debug panel to start the debugging session. This will launch Chrome and open the workbench URL.

4. Making Iterative Changes

As you work on your web part, you can make changes to your code, and Gulp will automatically compile and refresh the workbench:

  • Simply save your changes in your code editor.
  • Gulp will detect changes and rebuild the project.
  • Refresh the local workbench in your browser to see the updates.

Common Gulp Commands for Development

  • gulp serve: Starts the local development server.
  • gulp build: Builds your project for production.
  • gulp bundle: Bundles your web part.
  • gulp package-solution: Packages your solution for deployment.

5. Deploying Your Web Part

Once you are satisfied with your web part, you can deploy it to your SharePoint site:

  1. Run the following command to create a deployment package:
   gulp package-solution

This command will create a .sppkg file in the sharepoint/solution folder of your project.

  1. Upload the .sppkg file to your SharePoint App Catalog.
  2. Add your web part to a SharePoint page to see it in action.

6. Conclusion

You have successfully set up local debugging for your SPFx development environment. By using Gulp and browser developer tools, you can efficiently develop and debug your web parts, ensuring they function as expected before deployment. In your next steps, consider learning more about integrating PnPjs for interacting with SharePoint data, and explore Fluent UI components to enhance your web part’s user interface.

Edvaldo Guimrães Filho Avatar

Published by

Categories:

Leave a comment