Install Node.js and NPM on Ubuntu

Node.js is a free, open-source, cross-platform JavaScript runtime environment for server-side JavaScript execution, used for web and networking applications, and includes NPM (Node Package Manager), a command-line tool for managing Node.js packages, which is installed with Node.js.
Prerequisites
AWS Account with ubuntu
SSH access with sudo privileges
Firewall Port: 3000
Let's update and upgrade the system packages.
sudo apt-get update
sudo apt-get upgrade
Add Node.js and npm from NodeSource Repository
We have to install curl and add Node.js from Nodesource repository.
sudo apt-get install curl
How to Install Node.js and NPM on Ubuntu 20.04 LTS
Once the repository is added in Ubuntu, enter the command below to install Node.js and NPM.
sudo apt-get install -y nodejs
Check Node.js and NPM Version on Ubuntu
Check the Node.js version by entering the command below.
node --version
Output:
ubuntu@ip-172-31-88-89:~$ node --version
v18.19.1
To install npm.
sudo apt install npm
To check the NPM version, use the command npm --version.
npm --version
Output:
ubuntu@ip-172-31-88-89:~$ npm --version
9.2.0
Testing Node.js Server
Create a JavaScript file named nodeapp.js and add the code below to print "Hello World."
sudo vi nodeapp.js
Paste the following lines into it.
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}/`);
});
Use the following command to run your Node.js server.
node nodeapp.js
Output:
$ node nodeapp.js
Server running at http://127.0.0.1:3000/
Open your browser, enter the server name or IP followed by port 3000, and you will see the "Hello World" message.
http://127.0.0.1:3000
Here, we have successfully,To Install Node.js and NPM on Ubuntu
Happy Learning…!
