Skip to main content

Angular Debugging in MS Visual Studio Code

To debug a front-end application developed in JavaScript has been easy using Developer Tools of various popular browsers. But as the web is evolving, so does the Angular world. As you know, developing applications in Angular with TypeScript programming language is very useful for the developers who come from Object Oriented environment.

But to counter the problems that occur while developing applications, you must be equipped with a good editor and debugger tools. Visual Studio Code from Microsoft has proved to be a right tool for front-end developers with its ever growing list of extensions. In this article, we will see how to use VS Code editor for debugging Angular application step-by-step.

1. Chrome Browser

Make sure you have the latest version of Chrome browser installed on your machine. This is required because the extension that we will be using in VS Code will be of Chrome.

2. Chrome Debugger Extension

You need to install Chrome Debugger extension in your VS Code editor. The shortest way to install is to visit Chrome Debugger in Microsoft's market place. If you don't have VS Code, then it will prompt you to install it before you go ahead with installation of extension. Recommended version is 3.1.4.



3. Angular CLI

Install the latest Angular CLI version using NPM. Recommended version is 1.3 or greater. Execute below command from your Command Prompt to install Angular CLI globally:

npm install -g @angular/cli@">=1.3"

4. Create New Application

To create a new application with skeleton is very easy using Angular CLI. Let's create a demo application called DemoApp.

ng new demo-app

It will start installing all the dependencies and may take some time to finish it.



5. Open VS Code

Though you can open VS Code editor directly, there is a command to do the same thing. Change to the newly created application directory and open VS Code:

cd demo-app
code .



6. Configure Launch.json File

Click on the Debugging icon in the Activity Bar to bring up the Debug View. Then click on the gear icon to configure a launch.json file, selecting Chrome for the environment. By default it will create the file with below code:

{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "chrome",
            "request": "launch",
            "name": "Launch Chrome against localhost",
            "url": "http://localhost:8080",
            "webRoot": "${workspaceRoot}"
        }
    ]
}

Replace the above code with:

{
    "version": "0.2.0",
    "configurations": [
      {
        "name": "ng serve",
        "type": "chrome",
        "request": "launch",
        "url": "http://localhost:4200/#",
        "webRoot": "${workspaceRoot}"
      },
      {
        "name": "ng test",
        "type": "chrome",
        "request": "launch",
        "url": "http://localhost:9876/debug.html",
        "webRoot": "${workspaceRoot}"
      },
      {
        "name": "ng e2e",
        "type": "node",
        "request": "launch",
    "program": "${workspaceRoot}/node_modules/protractor/bin/protractor",
        "protocol": "inspector",
        "args": ["${workspaceRoot}/protractor.conf.js"]
      }     
    ]
  }

Above are the three objects for different purposes. First one is for application, second is for testing and third for end-to-end configuration. Below are the meanings of the attributes:

name 
Any friendly name which appears in Debug Configuration dropdown.

type
The type of debugger to use for this launch configuration. The drop down will show the ones for which you have already installed the extensions. In our case it is showing two - Node and Chrome - because Node comes by default and Chrome extension was just installed by us. You can have as many as you want for different languages like PHP, Go, DART, etc.

request
The request type of this launch configuration. Currently supported ones are launch and attach. Launch is used when there is no existing debugging process running and attach can be used when you want to attach the debugging to the existing running process.

url
The path to which the application will open up.

webroot
Supply the path of the folder opened in VS Code.

VS Code supports the variable substitution inside the strings of launch.json file and has following predefined variables:

${workspaceRoot} - the path of the folder opened in VS Code
${workspaceRootFolderName} - the name of the folder opened in VS Code without any slashes (/)
${file} - the current opened file
${relativeFile} - the current opened file relative to workspaceRoot
${fileBasename} - the current opened file's basename
${fileBasenameNoExtension} - the current opened file's basename with no file extension
${fileDirname} - the current opened file's dirname
${fileExtname} - the current opened file's extension
${cwd} - the task runner's current working directory on startup
${lineNumber} - the current selected line number in the active file

7. Start Debugging

As the configuration is completed, you can start debugging your application. To do so, set the breakpoint where you want the application to break before it proceeds. In our example, we will set it in app.component.ts file as shown below:



Now, open a terminal at the root folder (demo-app) and serve the application using Angular CLI. Execute below command:

npm start

You can also use ng serve but above command will make sure that the Angular CLI version is being used which is mentioned in the package.json file.



Go to the Debug View and select ng serve from the drop down menu and hit F5 key or click green play button to start debugging.



The names in the drop down menu are the ones which you have defined in the launch.json file.

Once the debugging is started, the application will launch in Chrome browser. It will not hit the breakpoint at first, so, refresh the page and you will be brought to the VS Code editor to the breakpoint.

After refreshing the page:



In VS Code editor:



On the left hand side you can see the call stack, local variables, etc which are very useful while debugging an application.

Hope you liked this article and if you have any comments please feel free to ask.

Comments

Popular posts from this blog

Characteristics of a Good Programming Language

Till now there are many high level languages which are very popular, and there are others, which could not become so popular in-spite of being very powerful. There might be many reasons for the success of a language, but one obvious reason is the characteristics of the language. Several characteristics believed to be important with respect to making a programming language good are briefly discussed below. Simplicity A good programming language must be simple and easy to learn and use. For example, BASIC is liked by many programmers only because of its simplicity. Thus, a good programming language should provide a programmer with a clear, simple and unified set of concepts which can be easily grasped. It is also easy to develop and implement a compiler or an interpreter for a programming language that is simple. However, the power needed for the language should not be sacrificed for simplicity. The overall simplicity of a programming language strongly affects the readability of the pr...

Angular 4 and Firebase Authentication: Setup

If you have come to this article, it means you are keen to learn new technology and that too Firebase. I love Firebase because it provides all basic but important features of any web application. Take for example, authentication, which is cumbersome and risky if not implemented with utmost care. And when we have the integration of Angular and Firebase, it becomes a lot easier for a developer to build such crucial modules in less time with minimal efforts. In this article we will create a simple Angular application using Firebase. I am going ahead with the understanding that you know the benefits of Firebase and have little knowledge about it. There are plenty of posts out on the web if you are behind. Step 1: Install Angular CLI (if not installed). npm install -g angular-cli Step 2: Create new Angular 4 project. By default now angular CLI will create Angular 4 project so you need not fret. ng new firebase-authentication Step 3: Check whether the ne...

Switch-case, simple calculator . . .

11.      Write a program to perform some mathematic functions with the help of “switch-case” statement. #include<stdio.h> #include<conio.h> void main() { int a,b,ans; char c; clrscr(); printf("\ Enter a and b:"); scanf("%d%d",&a,&b); printf("\n Enter operator:"); fflush(stdin); scanf("%c",&c); switch(c) { case'+':ans=a+b;       break; case'-':ans=a-b;       break; case'*':ans=a*b;       break; case'/':ans=a/b;       break; } printf("\n ans %d",ans); getch(); } 12.      Print the Fibonacci series #include<stdio.h> #include<conio.h> void main() { int a=1,b=1,s=3,c,ch; clrscr(); printf("how many steps="); scanf("%d",&ch); printf("%5d%5d",a,b); for(s=3;s<=ch;s++) {       c=a+b;       printf("%5d",c);       a=b;    ...