Node.Js is Server Side JavaScript runtime environment which Runs on Google’s V8 JavaScript Engine. Node.js is open-source and cross-platform which is licensed under MIT.
Node’s is an Evented I/O for JavaScript and its goal is to provide an easy way to build scalable network programs.
In this post we will see how we can How to Install Latest Nodejs with Npm on Ubuntu 20.04 , Ubuntu 18.04 or Ubuntu 16.04 LTS operating system. This method can also be used for other Debian and Ubuntu based Linux distributions including: Linux Mint, Debian, Kali Linux Edition (LMDE), elementary OS and others.
Option 1 . Install Node.js from the Ubuntu repository
$ sudo apt update $ sudo apt install nodejs
Check Node.js
First check installed Node.js version by following command
$ node -v v10.19.0
First check installed npm version by following command
$ sudo apt install npm $ npm -v 6.14.4
Option 2 . Installing Using a PPA or Installing Node.js with Apt Using a NodeSource PPA
Step 1 – Installing Prerequisites
We will need curl to add node.js ppa in our system. In addition we will also need to install python-software-properties package if its is not installed already.
$ sudo apt-get install curl
Step 2 – Adding NodeJs PPA
$ curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash -
Step 3 – Installing Node.js and NPM
After adding the node.js ppa we just need to give the node.js install command to install node.js. Npm will be installed automatically with node.js.
$ sudo apt-get install -y nodejs
Step 4 – Check Node.js
First check installed Node.js version by following command
$ node -v v14.2.0
First check installed npm version by following command
$ npm -v 6.14.5
Create Simple Node.js server
Create a javascript file and name it as app.js
Now we will add a code for creating a simple Node.js sever which prins Hello World. Just add the following javascript code in your app.js file.
const http = require('http'); const hostname = '127.0.0.1'; const port = 3000; const server = http.createServer((req, res) => { res.statusCode = 200; res.setHeader('Content-Type', 'text/plain'); res.end('Hello World\n'); }); server.listen(port, hostname, () => { console.log(`Server running at http://${hostname}:${port}/`); });
Node Run your Node.js server with the following commands
$ node app.js Server running at http://127.0.0.1:3000/
Node simply open the url http://127.0.0.1:3000/ in you browser.
it will print hello world
How to Install Nodejs and Npm on Ubuntu Linux
Leave a Reply