My Node app ran fine on my laptop. Getting it to run on a real server took 3 commands. Keeping it running took one more tool.
This post is part of my series on deploying a Node.js app to AWS. By now I have an Ubuntu EC2 instance, Node.js 22 and Git installed, and my project cloned from GitHub. If you're not there yet, the earlier steps are linked at the end.
1. Find the project folder
SSH into the server and list what's in your home folder:
ls My project is called node-api. Move into it:
cd node-api 2. Install the dependencies
The node_modules folder is never in Git, so the server doesn't have it yet. Install it:
npm i If this fails, check your Node version first with node -v. An old Node version is the most common cause. I'm on Node 22.
3. Start the app
npm start My app prints that it's listening on port 4000. It works.
If you want to open it from your browser using the server's public IP, that port also has to be allowed in the instance's Security Group.
4. The problem
Now press Ctrl+C. The app stops right away.
Close the SSH window and the same thing happens. The app is tied to your terminal session. When the session ends, the app ends.
That's fine for a quick test. It's useless for a real server that needs to stay up all day while you're logged out.
5. The fix: PM2
PM2 is a process manager for Node.js. It runs your app in the background, restarts it if it crashes, and can start it again after the server reboots.
I set it up in the next post and video.
Video
Here's the 2.5 minute walkthrough of everything above:
Next, keep it running 24/7 with PM2: https://youtu.be/aWgdEx5-w1k
Full course playlist: https://www.youtube.com/playlist?list=PLfeDvEEUyoEJZGnw11rKIET5lJDs6JmhD
Earlier steps:
- Launch and connect to EC2: https://youtu.be/JH5V5jKzILs
- Create the Security Group first: https://youtu.be/3tn56NQ2710
- Install Node.js 22 and Git on EC2: https://youtu.be/ixzhrVpSSlU
- Connect EC2 to GitHub with SSH keys: https://youtu.be/au71kakla4M
- Clone a private GitHub repo on EC2: https://youtu.be/1MDz3yMzj7A
