Menu Search Sign up

Building Tools

Node.js, NPM, NVM

https://nodejs.org/en/docs/ 

https://www.npmjs.com/

Node.js is an open source server framework
Node.js runs on various platforms (Windows, Linux, Unix, Mac OS X, etc.)
Node.js uses JavaScript on the server
Node.js is a platform built on Chrome's JavaScript runtime

Npm is a JavaScript package manager.
The npm registry hosts over a quarter million packages of reusable (mostly javascript) code.
Npm is an app written entirely in JavaScript and depends very heavily on Node.js APIs.

Nvm is a Nodejs version manager. It let's you easily install and switch between versions. It retains globally installed packages for each version.

Nodejs & NPM Download and Installation (on mac)

1. Download Node.js from https://nodejs.org/en/ (node-v6.9.1.pkg for mac users)

2. Install Node.js, choose to install for all users on the mac.

3. Install Node.js at /usr/local/bin/node, and npm at /usr/local/bin/npm (soft link to /usr/local/lib/node_modules/npm)

4. Make sure that /usr/local/bin is in your $PATH.

Nodejs & NPM Download and Installation (on windows)

1. Download Node.js from https://nodejs.org/en/ (https://nodejs.org/dist/v4.6.0/node-v4.6.0-x64.msi for windows users)

2. Click node-v4.6.0-x64.msi to install Node.js

3. On Windows, open a command line window, and navigate to the folder where nodejs is installed

4. Nodejs comes with npm installed so you should have a version of npm. However, npm gets updated more frequently than Nodejs does, so you'll want to make sure it's the latest version.

npm install npm@latest

Do not use the -g parameter. This will ensure the latest npm modules will be put under your nodejs folder.

Running Node.js as a Web Server (on MAC)

$ cd /usr/local/bin

$ sudo npm install http-server -g

This will install the http-server package onto:

/usr/local/lib/node_modules/http-server/bin/http-server

With the following 2 soft links (pointing to same file above)

/usr/local/bin/hs

/usr/local/bin/http-server

Now you can run a server via the following commands:

$ cd MyApp (any directory where you have your html files)

$ http-server -c 5

And then you can access your html files from http://localhost:8080

The -c option is to specify a cache timeout without which your changes to your html files will not be reflected to the browser right away (because default c value is 3600 or 1 hour).

Running Node.js as a Web Server (on Windows)

$ cd ./nodejs

$ npm install http-server -g

This will install the http-server package onto:

C:\Users\myname\AppData\Roaming\npm\node_modules\http-server

Now you can run a server via the following commands:

$ cd MyApp (any directory where you have your html files)

$ http-server -c 5

And then you can access your html files from http://localhost:8080

The -c option is to specify a cache timeout without which your changes to your html files will not be reflected to the browser right away (because default c value is 3600 or 1 hour).

NPM http-server limitations

It can only serve GET requests, it does not support POST requests. But apache can. So just come up with a static something.json file, put it into your apache folder, and your POST requests will be served.

NPM install command

If you have a package.json file in your directory and you run

npm install

npm will look at the dependencies that are listed in that file and download the latest versions, using semantic versioning.

cd C:\eclipse\workspace\ecr-emc-manulink_wf\node_modules
npm install @angular/compiler --save

You will see the above modules in C:\eclipse\workspace\ecr-emc-manulink_wf\node_modules

What does the --save and --save-dev flags do in above commands?

The --save and --save-dev install flags
The easier (and more awesome) way to add dependencies to your package.json is to do so from the command line, flagging the npm install command with either --save or --save-dev, depending on how you'd like to use that dependency.

To add an entry to your package.json's dependencies:
npm install <package_name> --save
To add an entry to your package.json's devDependencies:
npm install <package_name> --save-dev

https://docs.npmjs.com/getting-started/using-a-package.json