Why do we have install Node.js for Angular 2.0?

node.jsAngular

node.js Problem Overview


I started a tutorial on Angular 2.0, the first step in setting up the workspace is installing Node.js and NPM.

Why do we have install Node.js for Angular 2.0?

I don't remember doing this for angular 1.X.

node.js Solutions


Solution 1 - node.js

Technically, Node.js and NPM are not needed to do Angular2 work. It does ease things though. Here's the main reasons I speculate are behind this choice:

  • CLI: Since a while now the de facto way to build and develop new Angular apps is to use the CLI tooling which relies on Node and NPM as well.

  • TypeScript: Examples are .ts, and you need to run a compiler step to get them into .js, which can be done on-the-fly easily with Node.js and NPM (plus it's a way of easily getting typing files);

  • Web Server: Serving your Angular SPA from a "real" albeit light web server prevents probably some nasty issues that come with checking your site using file:// links.

The Quickstart guide itself actually continues to mention some more concrete reasons as well:

> Here's what these scripts do: > > - npm start - runs the compiler and a server at the same time, both in "watch mode" > > - npm run tsc - runs the TypeScript compiler once > > - npm run tsc:w - runs the TypeScript compiler in watch mode; the process keeps running, awaiting changes to TypeScript files and re-compiling when it sees them > > - npm run lite - runs the lite-server, a light-weight, static file server with excellent support for Angular apps that use routing > > - npm run typings - runs the typings tool separately > > - npm run postinstall - called by npm automatically after it successfully completes package installation. This script installs the TypeScript definition files defined in typings.json

You can also have a look at the Quickstart source and further dive into where NPM is needed.


Footnote: there's a similar question about needing Node.js for AngularJS (1.x).

Solution 2 - node.js

Because Anglar2 is based on Typescript, Web Components and ES6 which need compilation for performance and broader browser support. Typescript is compiled to ES5 JavaScript and the other features require shims for backwards compatibility.

Since Typescript is a superset of JavaScript, and it's compiled to JavaScript anyway, you can write your code in plain JavaScript but it's not recommended.

For a more detailed explanation check out these videos on YouTube

Solution 3 - node.js

NodeJS gives you the tool npm that allows you to download libraries and packages you would use in Angular 2. From the shell you can go to your folder and type npm install to install dependencies you need to have installed to get your angular project going. It will make it easier for you! If you want a complete starter kit go to https://github.com/buckyroberts, you can fork or download the zip with all the starter files to get you going :)

Solution 4 - node.js

You do not need to use Node anywhere in production to use any front-end JavaScript framework, whether it is jQuery, AngularJS, ReactJS, Angular2, etc.

Angular2 can be used in isolation but to get and feel better development environment, angular2 should be used with nodejs and npm. Some of the nodejs modules helps you in web development.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser2572003View Question on Stackoverflow
Solution 1 - node.jsJeroenView Answer on Stackoverflow
Solution 2 - node.jsPeterView Answer on Stackoverflow
Solution 3 - node.jsuser5504353View Answer on Stackoverflow
Solution 4 - node.jsRohit LuthraView Answer on Stackoverflow