Angular is usually edited in Visual Studio Code editor. Their powers combined let you not only develop Angular app code but also debug it through the editor! let us introduce the Angular debugging in VS code in this article with an example.
- Step 1: Create an Angular application;
- Step 2: Install Debugger for Chrome (you could install other debuggers you like)
- Step 3: Configure Debug Environment;
- Step 4: Start Debugging;
Step 1 – Create an Angular application
We use the Angular CLI to create projects,
npm install -g @angular/cli // Install the Angular CLI
ng new my-app // Creat a new Angular app
cd my-app // change to the app directory
code . // Open VS Code for the current directory
Step 2 – Install Debugger for Chrome (you could install other debuggers you like, such as Firefox Debugger)
In VS Code environment: Right Click Manage (Left Bottom Corner) > Extensions (or Ctrl+Shift+X)
Open the Extensions view. Then type ‘chrome’ in the search box. You’ll see the extensions: Debugger for Chrome.
Press the Install button for Debugger for Chrome
Step 3 – Configure Debug Environment
In VS Code environment: Click Run Button on the left side (or Ctrl+Shift+D)
If running and debugging is not yet configured (no launch.json
has been created), VS Code shows the Run start view,
You can configure the environment by creating launch.json
file yourself manually, but you can do that automatically by Clicking the Run and Debug button to open the Select Environment dialog box,
Select Chrome, then VS Code will create an launch.json
You might need to change the port to what your app running on, say, 4200.
Step 4 – Start Debugging
Now, in VS Code, if you Click Run Button on the left side (or Ctrl+Shift+D), you will have a debugging environment like this,
Run the app from the Angular CLI (very important, otherwise the debugging will not work) by command,
ng server -o
You will see the app running,
Now, set your breakpoint, and run debug,
Once a debug session starts, the Debug toolbar will appear on the top of the editor.
- Continue / Pause F5
- Step Over F10
- Step Into F11
- Step Out Shift+F11
- Restart Ctrl+Shift+F5
- Stop Shift+F5
The debug session is on like this,
PS: We can also debug Angular applications without using any breakpoints and without using console.log. Instead, we will look at debugging features introduced with Angular 9 under the ng namespace variable, which is automatically available in development mode but not exposed when the application is running in production mode. ( read the article here : https://blog.angulartraining.com/how-to-debug-angular-applications-5fcc98900c52 )