How To Create Angular App Using Angular CLI

Before one year, I planned to create a simple Angular app but at the time I didn't have any idea about Angular CLI. So, I faced a lot of issues while configuring things. I learned a lot and spent days configuring the Angular app before I was finally done.
 
In this article, we will learn how to create an Angular app using Angular CLI. Angular CLI is just a command line interface that helps us create an Angular app very easily within two minutes.

To know more about Angular, please go through the given links.
Agenda
  • How to create an Angular app using Angular-CLI
  • How to use Angular CLI commands 
  • Create an app with component, module, and HTML.
  • Create HTML to show "Welcome to C# Corner, Angular app using Angular CLI by Bikesh Srivastava".
  • Explanantion of each Angular CLI command.
What is Angular CLI?

Angular CLI is "a command line interface to create angular app with webpack and other configuration". Angular CLI provides four to five commands to create Angular application easily. Some of the commands are given below.

Mobile Toolkit

For more details, go to the official site of Angular CLI - https://cli.angular.io/ 
 
Don't confuse with the "my-dream-app" word, it's just the project name. You can write anything. Angular CLI provides us a very powerful, simple and easy command with which we can create an Angular app within two minutes. So, I would like to suggest you use Angular CLI commands to create Angular apps.

If you are using Angular CLI, you don't need to configure anything extra. Just run single command and run the application.
 
Let's follow the below steps

Step 1

Install Node.js npm on your system. You can download it from the Node.js official site https://nodejs.org/en/. After the installation is completed, check if it is installed or not. For that, open CMD on your system and type "npm -v" followed by pressing the Enter key.  You can se the latest version of npm installed on your system.
 
Step 2

Open cmd on your system and type "npm install -g @angular/cli" and press Enter. The Angular CLI will be installed for all the users on the computer because I used "-g". If you don't want to apply Angular CLI to all the users, you can remove -g from command.
 
Now, we can run Angular CLI to create, serve, run etc....

These Angular CLI commands can be used now.
  • ng new project_Name
  • ng generate
  • ng serve 
  • ng Test,Lint,Format
ng new app_Name

Before running this command, be sure "npm install -g @angular/cli" is executed. You don't have any need to run it again and again. You can use ng new command on system cmd command and on VS code also. So now, I am going to create a simple application using VS code.
 
 

Open Visual Studio Code => Open terminal to run command. Then, choose a directory to create project. Here, Angular4 is my project name.

Press Enter and check if the Angular4 project is created on the desktop with related files. One more point - you don't need to run npm install command. Angular CLI will do that by deafult.
 
 

In this image, you can see that Angular CLI created an Angular app and then installed packages from npm.

Note- If you have already installed Git on your system, Angular CLI configures Git with your project. In the following image, all configuration in created and installed successfully.

 

ng serve / npm start 

Now, we are ready to run the app. So, we can use either of the commands "ng serve" or "npm start". The "ng serve" runs the application directly while the "npm start" command runs 'ng serve' in the background. See package.json for more details. 
 
Package.json
  1. {  
  2.   "name""angular4",  
  3.   "version""0.0.0",  
  4.   "license""MIT",  
  5.   "scripts": {  
  6.     "ng""ng",  
  7.     "start""ng serve",  
  8.     "build""ng build",  
  9.     "test""ng test",  
  10.     "lint""ng lint",  
  11.     "e2e""ng e2e"  
  12.   },  
  13.   "private"true,  
  14.   "dependencies": {  
  15.     "@angular/animations""^4.0.0",  
  16.     "@angular/common""^4.0.0",  
  17.     "@angular/compiler""^4.0.0",  
  18.     "@angular/core""^4.0.0",  
  19.     "@angular/forms""^4.0.0",  
  20.     "@angular/http""^4.0.0",  
  21.     "@angular/platform-browser""^4.0.0",  
  22.     "@angular/platform-browser-dynamic""^4.0.0",  
  23.     "@angular/router""^4.0.0",  
  24.     "core-js""^2.4.1",  
  25.     "rxjs""^5.4.1",  
  26.     "zone.js""^0.8.14"  
  27.   },  
  28.   "devDependencies": {  
  29.     "@angular/cli""1.2.7",  
  30.     "@angular/compiler-cli""^4.0.0",  
  31.     "@angular/language-service""^4.0.0",  
  32.     "@types/jasmine""~2.5.53",  
  33.     "@types/jasminewd2""~2.0.2",  
  34.     "@types/node""~6.0.60",  
  35.     "codelyzer""~3.0.1",  
  36.     "jasmine-core""~2.6.2",  
  37.     "jasmine-spec-reporter""~4.1.0",  
  38.     "karma""~1.7.0",  
  39.     "karma-chrome-launcher""~2.1.1",  
  40.     "karma-cli""~1.0.1",  
  41.     "karma-coverage-istanbul-reporter""^1.2.1",  
  42.     "karma-jasmine""~1.1.0",  
  43.     "karma-jasmine-html-reporter""^0.2.2",  
  44.     "protractor""~5.1.2",  
  45.     "ts-node""~3.0.4",  
  46.     "tslint""~5.3.2",  
  47.     "typescript""~2.3.3"  
  48.   }  
  49. }  
In package.json, you can add or remove dependencies,scripts.etc....
 
So now it is time to run the aaplication. Use npm start or ng serve in terminal or cmd. You should keep in mind that the default port for Angular CLI is http://localhost:4200/ and the default browser is Google Chrome. So, copy and paste the URL in Chrome browser and hit Enter. 
 
Output 
 


So, if you want to change the port number according to your choice, don't worry about that. I am here to tell you how we can change the default port from 4200 to anything.

Open Angular-cli.json in your project and add 1-3 lines of code. Suppose I want to chnage it from 4200 to 2500. See the code given below.
  1. {  
  2.     "defaults": {  
  3.         "serve": {  
  4.             "port": 2500  
  5.         }  
  6.     }  
  7. }  
 If you are not getting where we need to add this line of code, just see the complete code of Angular-cli.json.
  1. {  
  2.   "$schema""./node_modules/@angular/cli/lib/config/schema.json",  
  3.   "project": {  
  4.     "name""angular4"  
  5.   },  
  6.   "apps": [  
  7.     {  
  8.       "root""src",  
  9.       "outDir""dist",  
  10.       "assets": [  
  11.         "assets",  
  12.         "favicon.ico"  
  13.       ],  
  14.       "index""index.html",  
  15.       "main""main.ts",  
  16.       "polyfills""polyfills.ts",  
  17.       "test""test.ts",  
  18.       "tsconfig""tsconfig.app.json",  
  19.       "testTsconfig""tsconfig.spec.json",  
  20.       "prefix""app",  
  21.       "styles": [  
  22.         "styles.css"  
  23.       ],  
  24.       "scripts": [],  
  25.       "environmentSource""environments/environment.ts",  
  26.       "environments": {  
  27.         "dev""environments/environment.ts",  
  28.         "prod""environments/environment.prod.ts"  
  29.       }  
  30.     }  
  31.   ],  
  32.   "e2e": {  
  33.     "protractor": {  
  34.       "config""./protractor.conf.js"  
  35.     }  
  36.   },  
  37.   "lint": [  
  38.     {  
  39.       "project""src/tsconfig.app.json",  
  40.       "exclude""**/node_modules/**"  
  41.     },  
  42.     {  
  43.       "project""src/tsconfig.spec.json",  
  44.       "exclude""**/node_modules/**"  
  45.     },  
  46.     {  
  47.       "project""e2e/tsconfig.e2e.json",  
  48.       "exclude""**/node_modules/**"  
  49.     }  
  50.   ],  
  51.   "test": {  
  52.     "karma": {  
  53.       "config""./karma.conf.js"  
  54.     }  
  55.   },  
  56.   "defaults": {  
  57.     "serve": {  
  58.       "port": 2500  
  59.     },  
  60.     "styleExt""css",  
  61.     "component": {}  
  62.   }  
  63. }  
Highlighted code is updated by me to change the port number.

So now it is time to work more with the Angular app and enjoy.

For more details, go to the official site of Angular - https://cli.angular.io/