Skip to main content

Command Palette

Search for a command to run...

Install Node.js and NPM on Ubuntu

Updated
2 min read
Install Node.js and NPM on Ubuntu
A
Hi there! I'm a passionate AWS DevOps Engineer with 2+ years of experience in building and managing scalable, reliable, and secure cloud infrastructure. I'm excited to share my knowledge and insights through this blog. Here, you'll find articles on: AWS Services: Deep dives into core AWS services like EC2, S3, Lambda, and more. DevOps Practices: Best practices for CI/CD, infrastructure as code, and automation. Security: Tips and tricks for securing your AWS environments. Serverless Computing: Building and deploying serverless applications. Troubleshooting: Common issues and solutions in AWS. I'm always eager to learn and grow, and I hope this blog can be a valuable resource for fellow DevOps enthusiasts. Feel free to connect with me on [LinkedIn/Twitter] or leave a comment below!

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…!

More from this blog

CloudHub

94 posts

Hi there! I'm a passionate AWS DevOps Engineer with 2+ years of experience in building ,reliable, and secure cloud infrastructure. I'm excited to share my knowledge and insights through this blog.