Showing posts with label Angular. Show all posts
Showing posts with label Angular. Show all posts

Wednesday, October 9, 2019

angular - install angular cli on ubunut using npm


Installing angular cli on ubuntu using npm

~$ npm install -g @angular/cli
/home/ubuntu/.nvm/versions/node/v10.16.3/bin/ng -> /home/ubuntu/.nvm/versions/node/v10.16.3/lib/node_modules/@angular/cli/bin/ng

> @angular/cli@8.3.8 postinstall /home/ubuntu/.nvm/versions/node/v10.16.3/lib/node_modules/@angular/cli
> node ./bin/postinstall/script.js

? Would you like to share anonymous usage data with the Angular Team at Google under
Google’s Privacy Policy at https://policies.google.com/privacy? For more details and
how to change this setting, see http://angular.io/analytics. Yes

Thank you for sharing anonymous usage data. If you change your mind, the following
command will disable this feature entirely:

    ng analytics off

+ @angular/cli@8.3.8
added 244 packages from 185 contributors in 1178.698s
~$ 



Sunday, April 21, 2019

JavaScript - Angular - Installing Angular Material Module and dependencies

In order to install angular material module.  Let us install the following:

  • npm install @angular/material@6.4.7 --save
  • npm install @angular/cdk@6.4.7 --save
  • npm install @angular/animations@6.1.7 --save
  • npm install hammerjs@2.0.8 --save


Now, let us install the flex layout module:

$  npm install --save @angular/flex-layout@6.0.0-beta.18
npm WARN @angular/animations@6.1.7 requires a peer of @angular/core@6.1.7 but none is installed. You must install peer dependencies yourself.
npm WARN ajv-keywords@3.4.0 requires a peer of ajv@^6.9.1 but none is installed. You must install peer dependencies yourself.
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.2.8 (node_modules/fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.2.8: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})

+ @angular/flex-layout@6.0.0-beta.18
added 1 package in 11.902s




JavaScript - Angular - Installing Angular of a specific version

Installing Angular of a specific version using npm:

$ npm install -g @angular/cli@6.2.1
/home/ubuntu/.nvm/versions/node/v8.11.1/bin/ng -> /home/ubuntu/.nvm/versions/node/v8.11.1/lib/node_modules/@angular/cli/bin/ng
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.2.8 (node_modules/@angular/cli/node_modules/fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.2.8: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})

+ @angular/cli@6.2.1
added 5 packages, removed 6 packages and updated 27 packages in 11.412s


Now to check the options available with the ng command:

$ ng help
Available Commands:
  add Add support for a library to your project.
  build (b) Builds your app and places it into the output path (dist/ by default).
  config Get/set configuration values.
  doc (d) Opens the official Angular API documentation for a given keyword.
  e2e (e) 
  generate (g) Generates and/or modifies files based on a schematic.
  help Displays help for the Angular CLI.
  lint (l) Lints code in existing project.
  new (n) Creates a new directory and a new Angular app.
  run Runs Architect targets.
  serve (s) Builds and serves your app, rebuilding on file changes.
  test (t) Run unit tests in existing project.
  update Updates your application and its dependencies.
  version (v) Outputs Angular CLI version.
  xi18n Extracts i18n messages from source code.

For more detailed help run "ng [command name] --help"
$


To check for the version details:

$ ng --version

     _                      _                 ____ _     ___
    / \   _ __   __ _ _   _| | __ _ _ __     / ___| |   |_ _|
   / △ \ | '_ \ / _` | | | | |/ _` | '__|   | |   | |    | |
  / ___ \| | | | (_| | |_| | | (_| | |      | |___| |___ | |
 /_/   \_\_| |_|\__, |\__,_|_|\__,_|_|       \____|_____|___|
                |___/
    

Angular CLI: 6.2.1
Node: 8.11.1
OS: linux x64
Angular: 
... 

Package                      Version
------------------------------------------------------
@angular-devkit/architect    0.8.1
@angular-devkit/core         0.8.1
@angular-devkit/schematics   0.8.1
@schematics/angular          0.8.1
@schematics/update           0.8.1
rxjs                         6.2.2
typescript                   2.9.2
    
$ 

Now, let us setup one application:

$ ng new myNgApp2 --style=sass
CREATE myNgApp2/README.md (1025 bytes)
CREATE myNgApp2/angular.json (3653 bytes)
CREATE myNgApp2/package.json (1316 bytes)
CREATE myNgApp2/tsconfig.json (408 bytes)
CREATE myNgApp2/tslint.json (2837 bytes)
CREATE myNgApp2/.editorconfig (245 bytes)
CREATE myNgApp2/.gitignore (503 bytes)
CREATE myNgApp2/src/favicon.ico (5430 bytes)
CREATE myNgApp2/src/index.html (295 bytes)
CREATE myNgApp2/src/main.ts (371 bytes)
CREATE myNgApp2/src/polyfills.ts (3194 bytes)
CREATE myNgApp2/src/test.ts (642 bytes)
CREATE myNgApp2/src/styles.sass (80 bytes)
CREATE myNgApp2/src/browserslist (388 bytes)
CREATE myNgApp2/src/karma.conf.js (964 bytes)
CREATE myNgApp2/src/tsconfig.app.json (166 bytes)
CREATE myNgApp2/src/tsconfig.spec.json (256 bytes)
CREATE myNgApp2/src/tslint.json (314 bytes)
CREATE myNgApp2/src/assets/.gitkeep (0 bytes)
CREATE myNgApp2/src/environments/environment.prod.ts (51 bytes)
CREATE myNgApp2/src/environments/environment.ts (662 bytes)
CREATE myNgApp2/src/app/app.module.ts (314 bytes)
CREATE myNgApp2/src/app/app.component.sass (0 bytes)
CREATE myNgApp2/src/app/app.component.html (1141 bytes)
CREATE myNgApp2/src/app/app.component.spec.ts (1001 bytes)
CREATE myNgApp2/src/app/app.component.ts (213 bytes)
CREATE myNgApp2/e2e/protractor.conf.js (752 bytes)
CREATE myNgApp2/e2e/tsconfig.e2e.json (213 bytes)
CREATE myNgApp2/e2e/src/app.e2e-spec.ts (304 bytes)
CREATE myNgApp2/e2e/src/app.po.ts (208 bytes)
npm WARN deprecated @angular/http@6.1.10: Switch to @angular/common/http - see https://angular.io/guide/http
npm WARN deprecated istanbul@0.4.5: This module is no longer maintained, try this instead:
npm WARN deprecated   npm i nyc
npm WARN deprecated Visit https://istanbul.js.org/integrations for other alternatives.
npm WARN deprecated circular-json@0.5.9: CircularJSON is in maintenance only, flatted is its successor.

> node-sass@4.11.0 install /home/ubuntu/Documents/ngWorks/jogesh/myNgApp2/node_modules/node-sass
> node scripts/install.js

Cached binary found at /home/ubuntu/.npm/node-sass/4.11.0/linux-x64-57_binding.node

> node-sass@4.11.0 postinstall /home/ubuntu/Documents/ngWorks/jogesh/myNgApp2/node_modules/node-sass
> node scripts/build.js

Binary found at /home/ubuntu/Documents/ngWorks/jogesh/myNgApp2/node_modules/node-sass/vendor/linux-x64-57/binding.node
Testing binary
Binary is fine
npm WARN ajv-keywords@3.4.0 requires a peer of ajv@^6.9.1 but none is installed. You must install peer dependencies yourself.
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.2.8 (node_modules/fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.2.8: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})

added 1142 packages in 30.052s
    Successfully initialized git.
$ 

Now, let type the following command:
$ npm install
npm WARN ajv-keywords@3.4.0 requires a peer of ajv@^6.9.1 but none is installed. You must install peer dependencies yourself.
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.2.8 (node_modules/fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.2.8: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})

up to date in 8.378s


Now, let us type :

$ ng serve
** Angular Live Development Server is listening on localhost:4200, open your browser on http://localhost:4200/ **
 10% building modules 3/3 modules 0 active                         



The app is now available on localhost at: http://localhost:4200/

This is a very simple bare bones application to help us get started with angular. Now, if we check the git:

$ git log --oneline
e222cd0 (HEAD -> master) initial commit



Friday, October 5, 2018

JavaScript - Angular - Angular Project Structure

Below is the structure of a angular project as seen in Visual Studio Code Editor:


1) e2e
The first folder we have is e2e - which stands for end to end. In this folder we write the end to end tests for our application. These are automated tests which simulate real user.

2) node_modules:
We store all the third party libraries in this folder. This folder is purely for development. On compiling our application, parts of the 3rd party libraries are bundled together and deployed in our application. 

3) src:

This is the actual source code of our application. Inside this we have a
module:
app.module.ts

Component:
app.component.css
app.component.html
app.component.spec.ts
app.component.ts

Every application has at least one module and one component. 

i) assests:
In this folder we will store the static assets of our application. Image files, text files and icons go in here. 

ii) environments:
We store the configuration settings for differnt environments. We have one file for production environment:
environment.prod.ts
We have one file for the development environment:
environment.ts

There are some other files in the source folder (src): 
iii) favicon.ico:This is the favourite icon file. This is the icon displayed in the browser.

iv) index.html:This is very simple HTML file that contains our angular application. In this we do not have any references  to a script or style-sheet. The references will be added dynamically to this page.

v) main.ts:
This is the main file. This is a typescript file which is the starting point of our application. We know that in a lot of the programming languages we have the main method - starting point of a program. The same concept is used in the angular applications.  All we are doing here is - bootstrapping the main module of our application.

platformBrowserDynamic().bootstrapModule(AppModule)
.catch(err => console.error(err));

Which in this case, is the AppModule. When angular loads this module - everything starts from there.

vi) polyfills.ts:
This imports some scripts that angular requires for running. This is because, the angular framework uses features of JavaScript - that are not supported by the current version of JavaScript that are not supported in many browsers. So, polyfills.ts file fills the gaps between the JavaScript needed by Angular and the features supported by the currently available browsers.

vii) styles.css:
This is where we will add the global styles to our application. Also, each page or say, each component will have its own styles

viii) test.ts:
This file is used for setting up our testing environment.

Now, outside this source folder:
angular.json: This is the configuration file for angular projects. This is standard configuration - we don't have to worry about it for most part.

.editorconfig:
If we are working in a team environment. We want to make sure all team members have the same settings in this file. This is were we store the settings.

.gitignore:
This is for excluding certain files and folders from our git repository. It is a tool for managing and versioning your source code.

karma.conf.js: This is a configuration for karma test runner: https://karma-runner.github.io/2.0/index.html

package.json: This is a standard file that every node project has. It includes:
name
version
dependencies: All the dependent libraries start with @angular/ and after that we have the name of the library.

"dependencies": {
"@angular/animations": "^6.1.0",
"@angular/common": "^6.1.0",
"@angular/compiler": "^6.1.0",
"@angular/core": "^6.1.0",
"@angular/forms": "^6.1.0",
"@angular/http": "^6.1.0",
"@angular/platform-browser": "^6.1.0",
"@angular/platform-browser-dynamic": "^6.1.0",
"@angular/router": "^6.1.0",
"core-js": "^2.5.4",
"rxjs": "~6.2.0",
"zone.js": "~0.8.26"
},

First library here (@angular/animations) - it is for animations. If we are not planning to use animations in our project - then we can delete this here. In the future, if we add  dependencies we will add it here.

devDependencies: These are the libraries we require in order to develop this application. We do not need these to run the application on the production server.

"devDependencies": {
"@angular-devkit/build-angular": "~0.8.0",
"@angular/cli": "~6.2.3",
"@angular/compiler-cli": "^6.1.0",
"@angular/language-service": "^6.1.0",
"@types/jasmine": "~2.8.8",
"@types/jasminewd2": "~2.0.3",
"@types/node": "~8.9.4",
"codelyzer": "~4.3.0",
"jasmine-core": "~2.99.1",
"jasmine-spec-reporter": "~4.2.1",
"karma": "~3.0.0",
"karma-chrome-launcher": "~2.2.0",
"karma-coverage-istanbul-reporter": "~2.0.1",
"karma-jasmine": "~1.1.2",
"karma-jasmine-html-reporter": "^0.2.2",
"protractor": "~5.4.0",
"ts-node": "~7.0.0",
"tslint": "~5.11.0",
"typescript": "~2.9.2"
}


These are purely for a developer machine.

protractor.config.json: This is a tool for running end-to-end tests for angular.
tsconfig.json: This is a bunch of settings for your typescript compiler. Typescript compiler will look at this settings and based on this settings it will perform compilation into JavaScript that browsers can understand.

tslnt.json: This includes a number of settings for tslint. tslint is a static analysis tool for typescript code. It checks your typescript code for readability , maintainability and functionality errors.

This covers the basic structure of an angular project.


Reference:

Tuesday, October 2, 2018

JavaScript - Angular - Create Hello World Project


To create our first angular project. Type the following at the command prompt:

$ ng new my-angular-hello-world
CREATE my-angular-hello-world/README.md (1036 bytes)
CREATE my-angular-hello-world/angular.json (3692 bytes)
CREATE my-angular-hello-world/package.json (1328 bytes)
CREATE my-angular-hello-world/tsconfig.json (408 bytes)
CREATE my-angular-hello-world/tslint.json (2837 bytes)
CREATE my-angular-hello-world/.editorconfig (245 bytes)
CREATE my-angular-hello-world/.gitignore (503 bytes)
CREATE my-angular-hello-world/src/favicon.ico (5430 bytes)
CREATE my-angular-hello-world/src/index.html (306 bytes)
CREATE my-angular-hello-world/src/main.ts (373 bytes)
CREATE my-angular-hello-world/src/polyfills.ts (3194 bytes)
CREATE my-angular-hello-world/src/test.ts (642 bytes)
CREATE my-angular-hello-world/src/styles.css (80 bytes)
CREATE my-angular-hello-world/src/browserslist (388 bytes)
CREATE my-angular-hello-world/src/karma.conf.js (964 bytes)
CREATE my-angular-hello-world/src/tsconfig.app.json (166 bytes)
CREATE my-angular-hello-world/src/tsconfig.spec.json (256 bytes)
CREATE my-angular-hello-world/src/tslint.json (314 bytes)
CREATE my-angular-hello-world/src/assets/.gitkeep (0 bytes)
CREATE my-angular-hello-world/src/environments/environment.prod.ts (51 bytes)
CREATE my-angular-hello-world/src/environments/environment.ts (662 bytes)
CREATE my-angular-hello-world/src/app/app.module.ts (314 bytes)
CREATE my-angular-hello-world/src/app/app.component.css (0 bytes)
CREATE my-angular-hello-world/src/app/app.component.html (1141 bytes)
CREATE my-angular-hello-world/src/app/app.component.spec.ts (1043 bytes)
CREATE my-angular-hello-world/src/app/app.component.ts (226 bytes)
CREATE my-angular-hello-world/e2e/protractor.conf.js (752 bytes)
CREATE my-angular-hello-world/e2e/tsconfig.e2e.json (213 bytes)
CREATE my-angular-hello-world/e2e/src/app.e2e-spec.ts (318 bytes)
CREATE my-angular-hello-world/e2e/src/app.po.ts (208 bytes)

> node-sass@4.9.3 install /home/ubuntu/Documents/my-angular-hello-world/node_modules/node-sass
> node scripts/install.js

Cached binary found at /home/ubuntu/.npm/node-sass/4.9.3/linux-x64-57_binding.node

> circular-json@0.5.7 postinstall /home/ubuntu/Documents/my-angular-hello-world/node_modules/circular-json
> echo ''; echo "\x1B[1mCircularJSON\x1B[0m is in \x1B[4mmaintenance only\x1B[0m, \x1B[1mflatted\x1B[0m is its successor."; echo ''


\x1B[1mCircularJSON\x1B[0m is in \x1B[4mmaintenance only\x1B[0m, \x1B[1mflatted\x1B[0m is its successor.


> node-sass@4.9.3 postinstall /home/ubuntu/Documents/my-angular-hello-world/node_modules/node-sass
> node scripts/build.js

Binary found at /home/ubuntu/Documents/my-angular-hello-world/node_modules/node-sass/vendor/linux-x64-57/binding.node
Testing binary
Binary is fine
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.2.4 (node_modules/fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.2.4: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})

added 1081 packages in 29.916s
    Successfully initialized git.


This creates our first project folder. Now we open this folder in visual studio code editor. Now to load our application in the web server. We type the following at the command prompt:

$ ng serve
** Angular Live Development Server is listening on localhost:4200, open your browser on http://localhost:4200/ **
                                                                                          
Date: 2018-10-02T08:01:48.746Z
Hash: 6123a2c4b979c7ced5fe
Time: 10356ms
chunk {main} main.js, main.js.map (main) 10.7 kB [initial] [rendered]
chunk {polyfills} polyfills.js, polyfills.js.map (polyfills) 227 kB [initial] [rendered]
chunk {runtime} runtime.js, runtime.js.map (runtime) 6.22 kB [entry] [rendered]
chunk {styles} styles.js, styles.js.map (styles) 15.6 kB [initial] [rendered]
chunk {vendor} vendor.js, vendor.js.map (vendor) 3.28 MB [initial] [rendered]
ℹ 「wdm」: Compiled successfully.


This has loaded the application to the web server.

Now, we can access the following URL ( http://localhost:4200/ ) at the web browser.


Now, we have a live development server which is serving at localhost and port = 4200. Angular CLI compiled our application. 

Monday, October 1, 2018

JavaScript - Angular - Install Angular CLI

To create an angular project, we need to first setup our development environment. First let us install the latest version of NodeJS. Visit the nodejs website and download the LTS version:

Check the version of node at the terminal. 
$ node --version
v8.11.1

$ npm --version

5.6.0


The minimum version required to install angular applications is version 6.9. So we can proceeed with installing Angular CLI.

Angular CLI is a Command Line Interface which we use to create a new angular project.It:
generates the boiler plate code
creates deployable packages

To install angular cli. At the command prompt:

$ npm install -g @angular/cli
/home/ubuntu/.nvm/versions/node/v8.11.1/bin/ng -> /home/ubuntu/.nvm/versions/node/v8.11.1/lib/node_modules/@angular/cli/bin/ng
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.2.4 (node_modules/@angular/cli/node_modules/fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.2.4: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})

+ @angular/cli@6.2.3
added 242 packages in 23.945s
$ 

Now to check if the installation was successful. At the command prompt:

$ ng --version

     _                      _                 ____ _     ___
    / \   _ __   __ _ _   _| | __ _ _ __     / ___| |   |_ _|
   / △ \ | '_ \ / _` | | | | |/ _` | '__|   | |   | |    | |
  / ___ \| | | | (_| | |_| | | (_| | |      | |___| |___ | |
 /_/   \_\_| |_|\__, |\__,_|_|\__,_|_|       \____|_____|___|
                |___/
    

Angular CLI: 6.2.3
Node: 8.11.1
OS: linux x64
Angular: 
... 

Package                      Version
------------------------------------------------------
@angular-devkit/architect    0.8.3
@angular-devkit/core         0.8.3
@angular-devkit/schematics   0.8.3
@schematics/angular          0.8.3
@schematics/update           0.8.3
rxjs                         6.2.2
typescript                   2.9.2
    
$