Install Node.js On Windows And Ubuntu System

Install Node.js On Windows And Ubuntu System

In the previous article you understood that what actually node.js is, if you read that. Now I’m going to tell “How to install node.js in both windows and ubuntu system”. After installing it properly, we’ll make a simple node program to test our installation.

Install Node.js On Windows And Ubuntu System

Install Node.js In Windows System:

A very simple and straight forward way to install node.js in windows system is its Microsoft Installer or .msi file which can be download from node’s download page.
1. Download the node.msi file from here.

Download Node.js windows installer
2. Double click and run the downloaded msi file and get installed it on your windows.

Install Node.js in windows system
The Microsoft Installer of node.js installs both Node platform and NPM in your computer. NPM is Node Package Manager or module manager which helps us to install or include new modules in our node application according to the requirement. The default node installation directory in windows is C:\Program Files\nodejs.

Test Your Node.js Installation:

To check weather it is installed properly or not you can test it in command prompt. Open your command prompt from Start->Run->type cmd and hit enter. Now type node -v in cmd and hit enter. You will get the version of node.js installed on your system e.g. 0.8.17 or something else. Similarly type npm -v to test the npm installation. You will see the following screen.

Test Node.js Installation In Windows
If both are OK then congratulations you’ve get installed node.js perfectly in your windows system.

Install Node.js On Ubuntu System:

Installing node.js on Ubuntu is not so complicated or messy as people think about it. Open the terminal and execute all the following commands one by one and wait. This depends on your internet connection speed. Within a few minutes you will be using node.js on your Ubuntu system also.

Ubuntu Commands For Node.js Installation:

sudo apt-get install python-software-properties python g++ make
sudo add-apt-repository ppa:chris-lea/node.js
sudo apt-get update
sudo apt-get install nodejs npm

It installs current stable Node on the current stable Ubuntu. Quantal (12.10) users may need to install the software-properties-common package for the add-apt-repository command to work: sudo apt-get install software-properties-common

After executing all the commands successfully, go in your default or home directory from terminal and execute the same following commands to see the installed versions of node.js and npm.
$node -v
$npm -v
If both are OK and giving the version number of node and npm then you’ve successfully installed node.js on your Ubuntu computer also.

What are Node.js Modules ?

Node.js is a combinations of node packages. Actually it has a very managed module system by which we can install appropriate modules in node according to our application requirement. There are many node.js modules developed day by day by node lovers. It comes with some default core modules such as http and net. All the modules can be installed via NPM (Node Package Manager).

How to install A Node.js Module Via NPM?

To install a Node.js Module in your computer, just open command prompt (In Windows) or terminal (In Linux) and type the following command:
$npm install module_name (For Linux)
C:\Documents and Settings\Manish npm install module_name (For windows).
Something like below:

Install Node.js Module using NPM

This will install the node module in your main node installation directory.

Write A Very Simple Node Program To Test Our Node Installation:

Because this is an installation tutorial of node, so instead of taking a very complex example, I’m making a simple application to verify your Node installation. Follow the given steps below :

1. Create a new directory “nodeapp” in C: drive. You can take the appropriate name and create it in any location where you want to put your node application. But make sure you can easily reach this directory through command prompt.
2. Now create a new javascript file app.js in this directory and paste the following code in the file. Save it and come to command prompt.

var http = require('http'); //This includes the default http module in application
http.createServer(function (req, res) { //this creates the server running on appropriate port
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World\n'); //This writes "hello world" in the page.
}).listen(8080, 'localhost'); //write on what port you want to run the application
console.log('Server is running at http://localhost:8080');

3. Reach the directory nodeapp in command prompt and type the command “node app.js”.

Your console will be showing the line “Server running at http://localhost:8080″ and open the url http://localhost:8080 in your browser and see what happens?

You will see the page showing “Hello World”. Have you noticed that there is no need of Apache Web Server to run this url in browser? That’s all the magic of node.js. I’ll be continue with some other complex and useful node.js applications.