Debug Lightning Web Components
Lightning web components are built using standard HTML and JavaScript, and on the desktop they run in a variety of supported browsers. You can use standard browser and JavaScript debugging tools, like Chrome DevTools, to debug your components during development. To review mobile-specific features, use a mobile device or virtual device build.
The simplest way to run and debug a component during development is to create a Lightning Page and add your component to it. Then load the Lightning Page in your preferred browser and use your favorite inspection and debugging tools. LWC debugging is optimized for Chrome DevTools.
Execution Modes for Lightning Components
The Lightning Component framework executes in one of two modes: production and debug.
Let’s look at the differences between the execution modes.
By default, the framework runs in production mode. This mode is optimized for performance.
In debug mode, JavaScript code is easier to read and debug. The JavaScript source you see in DevTools looks similar to your uncompiled source code. Debug mode also adds more detailed output for some warnings and errors.
Enable Debug Mode in Salesforce
Enable debug mode to make it easier to debug JavaScript code.
Only enable debug mode for users who are actively debugging JavaScript. Salesforce is slower for users who have debug mode enabled.
To enable debug mode for users in your org:
If you see only a few lines of code in a JavaScript file, then debug mode is not enabled. If enabling debug mode doesn’t reset the code in the code editor, do a hard reload. With DevTools open, right-click the browser Reload button and then click Hard Reload.
To disable debug mode for a user, follow the preceding steps and click Disable instead of Enable.
To debug Lightning web components code, use Chrome DevTools.
Google Chrome includes a comprehensive suite of excellent web development tools, Chrome DevTools. These tools are great for debugging your Lightning web components.
To get started, load the simplest page possible that can run your Lightning component. Then open DevTools.
To quickly find which line of code is failing, in the Sources panel, enable the Pause on all exceptions option in the DevTools debugger panel before running your code.
Use debug mode to troubleshoot your wire adapters with unminified JavaScript and custom formatting.
The easiest way to debug your wire adapters is via Chrome DevTools.
To debug a wire adapter:
Recommended by LinkedIn
When you receive data via an @wire, your data is bound to a function or a property.
When you debug Lightning web components in an org that’s enabled to use Lightning Web Security, you’ll notice a few differences compared to orgs where it’s not enabled.
First, ensure that you enable debug mode for the best debugging experience.
Debugging techniques are the same for code compiled for LWS, but let’s walk through a few differences:
Source Code Location with LWS
As you debug Lightning web components with Chrome DevTools, your component source code is normally displayed in the Sources panel inside the lightning/n folder, as described in Find Your Code in the Sources Panel.
When your components run in an org where Lightning Web Security is enabled, look for the lws folder near the bottom of the Page pane. Expand the folder and see your component files organized under their namespace folder.
Here you see the c namespace and its components on the current page (1). The same compositionBasics.js file with LWS enabled is shown with source mapping (2).
Source mapping enables the compiled sandboxed code shown in the lws folder to appear similar to your components’ uncompiled source code. You can inspect it, use breakpoints, and so on. You must enable debug mode to see the source mapping and to enable breakpoints.
If your code in the Sources panel doesn’t look like your uncompiled source code, make sure the setting Enable JavaScript source mapping in Chrome DevTools is enabled. It’s enabled by default.
Debug Mobile Components
You can test mobile basics, like small screen sizes and responsive design, in a desktop browser using tools like Device Mode in Chrome DevTools. To review mobile-specific features, use the Salesforce Mobile app on a real mobile device.
With Lightning on Mobile, the desktop and mobile versions of Salesforce provide the same user interface: Lightning Experience. This means that, while it’s not a substitute for comprehensive testing on actual mobile devices, you can use your desktop browser to run and debug many aspects of your mobile Lightning apps and components.
Chrome DevTools Device Mode lets you simulate these aspects of mobile devices:
To learn more about Device Mode, refer to Simulate Mobile Devices with Device Mode in Chrome DevTools on the Google Chrome DevTools website.