Debugging Angular Apps Created With Angular CLI in WebStorm
Note: This post was updated in June, 2021.
Angular CLI can help us bootstrap a new Angular app with a ready-to-use TypeScript and Webpack configuration. In this post, you’ll learn how to start debugging an Angular CLI application that is running on the localhost.
- Create an application with Angular CLI
- Set the breakpoints in your code
- Run the application in the development mode
- Launch a debugging session
- Examine the suspended application
- Known limitations
Generate an application with Angular CLI
With WebStorm, you don’t have to download Angular CLI in advance – during project generation the IDE downloads and runs it using npx.
Select File | New Project from the main menu, then, in the New Project dialog, select Angular CLI as the project type, and specify the application name (blogAngular) and parent folder for it (WS).
When you click Create, WebStorm downloads and runs Angular CLI, which generates an app with an Angular-specific structure, and downloads all the required dependencies.
Learn more from Creating a new Angular application.
You do not need to define any run/debug configurations yourself, as WebStorm has already created two default ones for you:
- Angular CLI Server to launch your app in the development mode.
- Angular Application to debug your app.
Set the breakpoints in your code
WebStorm recognizes line breakpoints, exception breakpoints, and conditional breakpoints. To set a line breakpoint, click the gutter next to the executable line of code where you want the application to suspend. To remove a breakpoint, just click it.
Run the application in development mode
You can do this in several ways:
- Open the built-in Terminal and type npm run start.
- Double-click the start task in the npm tool window.
- Open your package.json file, click the Run Script gutter icon next to the start script, and select Run Angular CLI Server from the list.
- Select the Run Angular CLI Server run/debug configuration from the list on the toolbar and click the Run button next to the list.
In either case, WebStorm launches the predefined Angular CLI Server run/debug configuration. When the app is compiled and the Webpack dev server is ready, the Run tool window shows that the app is running in the browser at http://localhost:4200/.
Note that when the dev server is running, the app will automatically reload if you change any of the source files.
Launch a debugging session
You can now start debugging the client-side TypeScript code of your app right from the Run tool window! Just hold Ctrl+Shift / ⌘⇧ and click the URL address where the application is running. WebStorm launches the auto generated Angular Application run/debug configuration, the browser opens at http://localhost:4200/, and the Debug tool window appears showing you the call stack and variables.
Of course, you can also select the Angular Application run/debug configuration from the list on the toolbar and click the Debug button next to the list of configurations in the top right corner of the IDE.
Examine the suspended application
Once a breakpoint is hit, WebStorm opens the Debug tool window where you can explore the call stack and variables, step through the code, set watchers, evaluate variables, and do all the other things that you can normally do when debugging. You can learn more about this from Examine suspended program and Step through the program docs.
The breakpoints put in the code and executed on page load might not be hit when you open an app under a debug session for the first time. The reason for this is that the IDE needs to get the source maps from the browsers to be able to stop on a breakpoint you’ve placed in the original source, and that only happens after the page has been fully loaded at least once. As a workaround, you can reload the page in the browser.
If you have any problems with debugging your application, please contact our support team. Please share information about your IDE version and breakpoint locations and, if possible, your project.
The WebStorm team