pdiddy
pdiddy

Reputation: 6297

Angular2 and node.js

I'm just beginning to read the angular2 quickstart and notice it is using npm, node and typescript. I was wondering what is the use of node.js in angular2? Isn't angular just a client side framework? So what is the use of node.js and what is node.js exactly doing? In the quickstart, it doesn't show you how to use angular2 without node.js. So I'm a bit confused since in my mind angular is just a client side framework.

Thanks for the clarification.

Upvotes: 3

Views: 1236

Answers (3)

Thierry Templier
Thierry Templier

Reputation: 202138

The reason I think that Angular2 is provided as an NPM module is because of the TypeScript compilation.

The compiler requires d.ts files for module contracts. Moreover when you specify the moduleResolution attribute to node in the tsconfig.json file, it will load them through Node:

{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "moduleResolution": "node", // <-----
    "sourceMap": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "removeComments": false,
    "noImplicitAny": false
  },
  "exclude": [
    "node_modules",
    "typings/main",
    "typings/main.d.ts"
  ]
}

That being said, Node isn't used within the Angular2 application itself when executing within the browser.

Upvotes: 0

Nivesh
Nivesh

Reputation: 2603

Fact: Sure, angularJS is a client side framework. But NODEJS is not a sever side framework, it is a platform which provides JavaScript Runtime Environment.

Why NodeJS is being used in Client Side Development:

  • It provides instant access to all the JS libraries(e.g Angular) to be used in the web page, with the help of npm (Node Package Manager). Thus, install any required library using npm install package_name --save. So, no need to learn bower.
  • One can write build scripts and create watchers. Thus, no need of Gulp or Grunt.
  • One can install any live-reload node module for live reload the changes made in your client app.

NODEJS used in Angular2 (JavaScript) Documentation:

  • Created a package.json file which contains all the required libraries for the client app.
  • Next simply hit npm install and all the required libraries are downloaded in your local machine under node_modules folder.
  • thus: <script src="node_modules/@angular/core/core.umd.js"></script>
  • Installs a node module called: lite-server which serves your web app and live reloads it on changes. It is executed by npm start check the script section in package.json.
  • Thus, for futher development you just need to focus on your app and for downloading any client side libraries one can use like: npm install jQuery --save

At the end of the day, NODEJS just makes your life easier with just one package.json file.

Upvotes: 3

Zdravko Tatarski
Zdravko Tatarski

Reputation: 184

Angular2 is a client side framework, it doesn't need nodejs in order to work. The confusion comes from typescript, because the typescript compiler runs on nodejs. Note that typescript supports ES6 which introduced the support for modules that have import and export functionality.

NodeJS became inseparable part of the front-end development cycle since it provides many tools for building apps, task runners, compilers, test frameworks etc. .

PS: There is an angular documentation dedicated for javascript: link

You can switch the languages from the dropdown menu under the main header. Sadly, at the moment most of the documentation is written only for Typescript. You can download already compiled source of angular2 and start using it without node.

Upvotes: 7

Related Questions