Set Up a Node.js Development Environment on Amazon EC2

The terminal says “Listening on port 3100,” but your browser keeps waiting at the EC2 address. I like that the sample answers with a plain-text line, so there’s a clear result to recognize when the app responds.

Express creates the reply, but getting a request to it is a separate part of the setup. The context that makes that distinction click is what the EC2 instance is actually running.

What runs on an EC2 development instance

An Amazon Elastic Compute Cloud (EC2) Node.js development environment is a Linux virtual machine with Node.js and the Node Package Manager (npm) installed, plus network rules for its app. Node.js runs the JavaScript process, npm installs project packages, and an EC2 security group controls which inbound connections can reach the instance.

Express can answer locally while a browser request times out, because the app listener and the Virtual Private Cloud (VPC) route or security-group rule are separate checks.

LayerWhat it doesWhere you check it
Node.js and ExpressRun the app and answer Hypertext Transfer Protocol (HTTP) requestsOn the instance with curl
EC2 security groupAllows or blocks inbound Transmission Control Protocol (TCP) trafficIn the instance’s security-group rules
VPC route and public Internet Protocol version 4 (IPv4) addressProvide a path between the instance and the internetIn the instance and subnet network details

I ran the sample with Node.js v24.21.0 and Express 5.2.1. The terminal screenshot records npm 11.19.0, while AL2023 gets its own maintained patch releases from the repository.

Node.js v24.21.0 and npm 11.19.0 version checks
The local sample environment reports Node.js v24.21.0 and npm 11.19.0.

Check the instance and network access

The commands below target Amazon Linux 2023 (AL2023), so choose that AMI and review the EC2 pricing details before launch because the instance and storage selections affect charges.

  • Choose the Amazon Linux 2023 Amazon Machine Image (AMI) in the EC2 launch wizard.
  • Choose a key pair for Secure Shell (SSH), or use EC2 Instance Connect when it is configured for your instance.
  • Allow inbound SSH on TCP port 22 only from your public IP address. Do not open SSH to every address.
  • For browser access, the instance needs a public IPv4 address and a route through an internet gateway. The EC2 IP-addressing guide explains public addresses, and the VPC internet-gateway guide covers the route. Add a security-group rule for the app’s port only for the test.

AWS’s EC2 setup guide uses AL2023, and the security-group guide describes inbound rules as a virtual firewall. Allow SSH from your IP first, then add TCP 3100 only while testing this app.

Install Node.js and npm

The AL2023 repository supplies namespaced Node.js package tracks, so this setup selects the Node.js 24 LTS executable rather than relying on the system default.

Step 1: Connect to the Linux instance

Copy the instance’s public DNS name from the EC2 console and set the downloaded private key to user-only access. Then connect as ec2-user, the default login for Amazon Linux.

chmod 400 my-key.pem
ssh -i my-key.pem ec2-user@PUBLIC_DNS

Replace my-key.pem with the key file’s path and PUBLIC_DNS with the value shown for your instance. If SSH times out, check the inbound port 22 rule and the instance’s status checks before changing the key.

Step 2: Install the AL2023 Node.js packages

Dandified YUM (DNF) installs Node.js with its matching npm package on Amazon Linux 2023.

sudo dnf install -y nodejs24 nodejs24-npm

The AL2023 Node.js documentation names node-24 and npm-24 as versioned executables. Those commands select this major directly, so they do not rely on whichever version the alternatives tool made the default.

Step 3: Confirm the runtime and package manager

Check both executable versions before creating the app and confirm node-24 reports major version 24 with npm-24 available.

node-24 --version && npm-24 --version

The node-24 command selects this runtime major, and npm-24 invokes its paired package manager. If either is missing, confirm both AL2023 packages installed before creating the project.

Run and test an Express app

Express handles the HTTP request in this sample, while Node.js runs the app process. Install Express in a project directory so npm records it as a project dependency instead of adding it to the whole instance.

Step 4: Create the project

Create a directory, initialize package.json, and install Express from npm.

mkdir ec2-node-demo
cd ec2-node-demo
npm-24 init -y
npm-24 install express

npm init writes the package manifest, and npm install adds Express under the project’s dependencies. Keep the project directory open for the next file.

Step 5: Listen on a test port

Save this as app.js. It binds to all network interfaces on port 3100, while the security group still decides whether a remote client can reach that port.

const express = require('express');

const app = express();
const port = 3100;

app.get('/', (_request, response) => {
  response.type('text/plain').send('Hello from EC2');
});

app.listen(port, '0.0.0.0', () => {
  console.log(`Listening on port ${port}`);
});

I kept the response as plain text so curl has one body value to match.

The 0.0.0.0 bind makes the process listen on the instance’s network interfaces. It does not open the EC2 firewall, so add an inbound TCP 3100 rule with your public IP as the source before testing from your computer.

node-24 app.js

Use a second SSH session to send curl to the loopback address, which tests Express inside the instance without involving the security group.

curl -i http://127.0.0.1:3100/

I received HTTP 200 and the body Hello from EC2 from that request. After the local check passes, open http://PUBLIC_IPV4:3100/ in your browser, replacing PUBLIC_IPV4 with the instance’s public IPv4 address.

Express returns HTTP 200 and Hello from EC2 on port 3100
A local curl request receives an HTTP 200 response from the Express endpoint.

Fix connection and package errors

A local HTTP response helps isolate the failure, so match the first unsuccessful check to the layer it points at.

SymptomCheckNext action
SSH connection times outInbound TCP 22 source and instance status checksAllow your current public IP and wait until the instance passes its checks.
Local curl cannot connectNode process and port 3100Start node-24 app.js in the project directory and keep that terminal open.
Local curl works, browser times outInbound TCP 3100 source, public IPv4 address, and subnet routeAllow port 3100 from your IP and confirm the instance has a public route through an internet gateway.
node-24 or npm-24 is not foundAMI and package installationConfirm the instance runs Amazon Linux 2023 and that both nodejs24 packages finished installing.

The app’s bind address and an EC2 security-group rule control different parts of this path. If curl works locally, inspect the inbound rule and public route before editing code.

Keep the test rule’s source to your public IP, since 0.0.0.0/0 exposes the development server to every IPv4 address.

Close the temporary test port

This foreground Express process is a development check, not a production service. Press Ctrl+C in its terminal, remove the TCP 3100 security-group rule, and keep the inbound source restricted whenever you test again.

Run one more check against the project’s installed packages.

npm-24 ls --depth=0

If the app needs to start after a reboot or serve users continuously, add a process manager, TLS, and a reverse proxy as a separate deployment task. This sample keeps that boundary visible by opening one test port only while you need it.

Node.js on EC2 questions

The package commands depend on AL2023, while the foreground server runs only when you start it. These answers cover runtime choice and what happens after a restart.

Will the Express app start after the EC2 instance reboots?

No. Installing Node.js and Express does not register app.js as a service. Connect to the instance and run node-24 app.js again, or configure a supervised service in a separate deployment setup.

What if my project requires a different Node.js major version?

AL2023 offers versioned Node.js 20, 22, and 24 packages. Check the project’s required version first. If its required major is not available from the instance repository, use a version manager from its official documentation and make sure the selected version matches the project.

Pankaj Kumar
Pankaj Kumar

Pankaj Kumar is the founder and CEO of CodeForGeek, with more than 14 years in IT. He is an open-source enthusiast who enjoys sharing what he learns through CodeForGeek and YouTube, with a focus on Python, data analytics, machine learning, Angular, Node.js, and Kafka.

Articles: 337