AWS

How to Deploy a Node.js Application On AWS EC2 Server

JavaScript is basically a web-browser scripting language which means it is a client-side language. Node JS is an environment to run and execute the code written in the JavaScript language outside of the web browser. Developers often use Node to develop back-end services, also called Application Programming Interface (API). These are the services that power the applications used by the client that it sees and interacts with.

Let’s start with how to deploy a Node.js application on AWS EC2.

Deploy a NodeJS Application On AWS EC2 Server

To deploy Node JS application on AWS EC2, simply launch an EC2 instance from the instance page:

Type the name to identify the instance and select AMI to use it as a virtual machine:

Select the instance type that is free tier eligible to use it free of cost and then choose key pair file:

Keep other configurations as default and review the settings from the Summary to launch the instance:

After that, select the instance to connect to it:

Copy the command from the platform and paste in on the Terminal:

Paste the command on the Terminal and change the path of the key pair file and hit Enter:

Install node version manager (nvm) by typing the following at the command line:

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh | bash

The above command will install the nvm on the EC2 instance:

After that, activate the nvm by using the following command

. ~/.nvm/nvm.sh

Install the upgraded Node.js version by typing the following command:

nvm install node

Running the above command will install Node.js on the instance:

Use the following command to check the versions of Node and npm:

node -v
npm -v

The above command will display the following output:

Use the following command to update the apt packages:

sudo apt-get update -y

Running the above command will display the following output:

Install the git using the following command:

sudo apt-get install git -y

The above command will install the Git to clone the repository:

Check the version of the git using the following command:

git -version

The user will get the version of the Git:

Clone the Git repository containing the Node.js application using the following command:

git clone https://github.com/yeshwanthlm/nodejs-on-ec2.git

Running the above command will display the following output:

Get inside the directory using the following command:

cd nodejs-on-ec2

After that, install npm packages using the following command:

npm install

Running the above command will display the following output:

Start the npm to deploy the application using the following command:

npm start

Running the above command will start the npm:

Head into the EC2 console page to copy the Public IP address:

Paste the IP address on the web browser to visit the Node.js application:

You have successfully deployed a Node.js application on the AWS EC2 server.

Conclusion

To deploy the Node.js application on AWS, create and connect to an EC2 instance from the AWS platform. On the EC2 instance, install Node.js and npm after activating nvm. After that, install Git and clone the repository containing the Node.js application and then get inside the directory to install and start npm to deploy the Node.js application.

About the author

Talha Mahmood

As a technical author, I am eager to learn about writing and technology. I have a degree in computer science which gives me a deep understanding of technical concepts and the ability to communicate them to a variety of audiences effectively.