Installing Nodejs On Ubuntu & Windows

Hi Guys, Welcome to Proto Coders Point, In this Programmer Blog post we will learn About What is Node.js and Why is Node js used for? and even we will check how to install NODE.JS on Ubuntu & Windows OS.

What is Node.js?

Node.js is basically an Open-Source Scripting Language, It is and JavaScript runtime Environment that executes JS codes outside a browser also.

The Node js will help the Back-End Developer use javascript to write server-side script to produce dynamic web page and fetch data from database before web page is sent to user’s browers.

In Short Node.js is and Open Source server environment which is completely free to used and Node.js is very speed and easily compatible on various OS platforms (Windows, Linus , Unix, Mac OS any many more)

Why NodeJS is Famous?

By using Simple JavaScript, we cannot get access to files system from browser but sometimes there is a situation, where we need to be able to read data from local file system sinse this is not possible by using Javascript.

In this sense we need something that we can use to interact directly with the computer file system, that’s what exactly what node.js allows us to do.

Node.js allows us to take javascript out of the browser by allowing us to interact directly with the computer data source.

Thus not we can access system files, Therefore by using Node.js we can also create a desktop application and Atom Editor is the best example that is built using Node.js.

Then, Now you can write javascript code not just for browser but also to make use of hardware component or file system.

Why node.js?

Well, node.js can be run on your own computer or somebody else computer rather say it you can run server script using Node.js on server computer.

How to Install nodejs latest version on ubuntu

Here we use curl to download nodejs 16 on Ubuntu.

Option 1: Using curl

Installation Steps

1. Install curl on ubuntu

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
sudo apt install curl
sudo apt install curl
sudo apt install curl

2. Download NodeJS 16 Repository

curl -sL https://deb.nodesource.com/setup_16.x | sudo bash –

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
curl -sL https://deb.nodesource.com/setup_16.x | sudo bash -
curl -sL https://deb.nodesource.com/setup_16.x | sudo bash -
curl -sL https://deb.nodesource.com/setup_16.x | sudo bash -

In above cmd just replace setup_16.x to latest Eg: setup_17.x (whichever is latest).

3. Finally install Latest NodeJS using below cmd.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
sudo apt -y install nodejs
sudo apt -y install nodejs
sudo apt -y install nodejs

Finally we have successfully installed, Latest Nodejs On Ubuntu, just by following above 3 cmd both node & npm will get installed.

4. Check Node & npm version

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
node -v
npm -
node -v npm -
node  -v
npm -

Option 2: use standard Binary Package to install nodeJS & Npm in ubuntu

1. use wget to download the nodeJS of your desired version, Note that in below command replace v16.17.1 with the version you want to download.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
sudo wget https://nodejs.org/dist/v16.17.1/node-v16.17.1-linux-x64.tar.xz
sudo wget https://nodejs.org/dist/v16.17.1/node-v16.17.1-linux-x64.tar.xz
sudo wget https://nodejs.org/dist/v16.17.1/node-v16.17.1-linux-x64.tar.xz

2. to unpack or unzip the above tar.xz u need to xz-utils so install it.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
sudo apt-get install xz-utils
sudo apt-get install xz-utils
sudo apt-get install xz-utils

3. Now extract the tar.xz using xz-utils as below, This cmd will install node.js binary in /usr/local/:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
tar -C /usr/local --strip-components 1 -xJf node-v16.17.1-linux-x64.tar.xz
tar -C /usr/local --strip-components 1 -xJf node-v16.17.1-linux-x64.tar.xz
tar -C /usr/local --strip-components 1 -xJf node-v16.17.1-linux-x64.tar.xz

4. Finally Node and npm is been install in /usr/local/bin, To check run below commands

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
ls -l /usr/local/bin/node
ls -l /usr/local/bin/npm
or
node -v
npm -v
ls -l /usr/local/bin/node ls -l /usr/local/bin/npm or node -v npm -v
ls -l /usr/local/bin/node
ls -l /usr/local/bin/npm

or 

node -v
npm -v

How to Install Node.js on Windows

Installation Steps

  1. Download the Windows installer from the Nodes.js® web site
  2. Choose the LTS version that’s shown on the left. 
  3. Run the installer (the .msi file you downloaded in the previous step.)
  4. Follow the prompts in the installer (Accept the license agreement, click the NEXT button a bunch of times and accept the default installation settings).
node js installation
node js installation

5. Restart your computer. You won’t be able to run Node.js® until you restart your computer.

6. Confirm that Node has been installed successfully on your computer by opening a Hyper terminal and typing in the commands node --version

how to check nodejs version

You should see the version of node you just installed. So you can see my pc is been install with NodeJS version 12.17.0

Getting Started with NodeJS

Once Nodejs is been Installed successfully in your OS, Let’s use write a node script that will simply display ” HelloWorld” on the Browser.

Create a folder on your Desktop named “nodejs” under that folder Create a .js file named “helloworld.js”, and add the following code:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/h1'});
res.end("Hello World! Welcome to Proto Coders Point");
}).listen(8080);
console.log(" Server Running at : http://localhost:8080/");
var http = require('http'); http.createServer(function (req, res) { res.writeHead(200, {'Content-Type': 'text/h1'}); res.end("Hello World! Welcome to Proto Coders Point"); }).listen(8080); console.log(" Server Running at : http://localhost:8080/");
var http = require('http');

http.createServer(function (req, res) {
    res.writeHead(200, {'Content-Type': 'text/h1'});
    res.end("Hello World! Welcome to Proto Coders Point");
}).listen(8080);

console.log(" Server Running at : http://localhost:8080/");

Save the file.

Then Open Comment Prompt and run the below command

1. moving inside folder.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
C:\Users\rajat>cd Desktop
C:\Users\rajat\Desktop>cd nodejs2
C:\Users\rajat>cd Desktop C:\Users\rajat\Desktop>cd nodejs2
C:\Users\rajat>cd Desktop

C:\Users\rajat\Desktop>cd nodejs2

2. run the script using node cmd

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
C:\Users\rajat\Desktop\nodejs2>node helloworld.js
C:\Users\rajat\Desktop\nodejs2>node helloworld.js
C:\Users\rajat\Desktop\nodejs2>node helloworld.js

Then,  your computer works as a server of helloworld.js file

If anyone tries to access your computer on port 8080, they will get a “Hello World!” message in return!

Start your internet browser, and type in the address: http://localhost:8080

how to run nodejs file in cmd prompt

Result on the browser

1 Comment