Why Node.js is missing
Jenkins runs inside a Docker container. The official jenkins/jenkins:lts image does not include Node.js. If your Execute Shell runs npm install, you get npm: not found.
You must either:
- Install Node.js inside the container (fine for learning), or
- Use a Docker-based agent / custom image (preferred later)
This lesson uses option 1 so Freestyle stays simple.
Installing packages inside a running container is temporary. If you recreate the container (Day 3 Docker socket mount), reinstall Node.js and PM2.
Step 1 — Recreate Jenkins with port 3001 (if needed)
If you already exposed 3001 on Day 1, skip stop/rm. Otherwise:
docker stop jenkins
docker rm jenkins
docker run -d \
--name jenkins \
-p 8080:8080 \
-p 50000:50000 \
-p 3001:3001 \
-v jenkins_home:/var/jenkins_home \
jenkins/jenkins:ltsThe jenkins_home volume keeps your jobs and plugins.
Step 2 — Install Node.js inside the Jenkins container
docker exec -u root -it jenkins bashInside the container:
apt update
apt install nodejs npm -y
node -v
npm -v
exitStep 3 — Install PM2 on the EC2 host (optional pattern)
In this lab, PM2 often runs inside the Jenkins container so the Freestyle shell can call pm2 during the build. Install it where your build steps run:
docker exec -u root -it jenkins bash
npm install -g pm2
exitRunning the app inside the Jenkins container is a learning shortcut. Production usually deploys to a separate host or Kubernetes. Day 3 switches to Docker containers for deploy.
Step 4 — Update the Freestyle Execute Shell
Edit my-first-job (or a new Freestyle job pointing at the same repo). Replace the shell with:
export PATH=$PATH:/usr/local/bin
echo "Build started!"
echo "Branch: $GIT_BRANCH"
echo "Build Number: $BUILD_NUMBER"
echo "Listing files..."
ls -la
echo "Navigating to project folder..."
cd project-1-freestyle
echo "Installing dependencies..."
npm install
echo "Running tests..."
npm test
echo "Build completed successfully!"
echo "Deploying app using PM2..."
if pm2 list | grep -q project-1-app; then
echo "App exists → Restarting..."
pm2 restart project-1-app
else
echo "App not found → Starting..."
pm2 start index.js --name project-1-app
fi
pm2 saveSave → Build Now → open Console Output.
You should see npm install, tests, and a PM2 start/restart.
If npm or pm2 is not found, re-check that you installed them inside the Jenkins container (as root), not only on the EC2 host.