How to Fix the “NestJS Not Running on Shared VPS: Config File Bug + Permission Hell” – 7 Urgent Steps I Learned the Hard Way
You’ve finally pushed your NestJS API to a cheap shared VPS, only to watch it die on launch with cryptic “cannot read config” errors. Your heart races, the client’s deadline looms, and the server logs look like a spelling‑bee in a dark room. Trust me – I’ve been there. After three sleepless nights and a handful of angry tickets, I cracked the exact combo of config‑file mistakes and Linux permission nightmares that keep most developers stuck.
Why This Matters
If your NestJS app can’t start on a shared VPS, you’re not just losing uptime – you’re losing credibility, potential revenue, and precious development time. A mis‑configured ormconfig.json or a wrong chmod can turn a $5/month server into a $0 profit project. Fixing it quickly means you keep the stack cheap, the client happy, and the money flowing.
7 Urgent Steps to Get Your NestJS App Running
-
Step 1 – Verify Node & npm Versions
Shared hosts often ship outdated Node.js. Run:
node -v npm -vIf you see
v12or lower, install a newer version withnvm(Node Version Manager):curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.5/install.sh | bash source ~/.bashrc nvm install 20 nvm use 20 -
Step 2 – Clean Up the Config Files
Most crashes stem from a malformed
ormconfig.jsonor a missing.env. Open the file and make sure:- All keys are double‑quoted.
- No trailing commas.
- Absolute paths are used for
entitiesandmigrations.
{ "type": "postgres", "host": "${DB_HOST}", "port": 5432, "username": "${DB_USER}", "password": "${DB_PASS}", "database": "${DB_NAME}", "entities": ["dist/**/*.entity{.ts,.js}"], "migrations": ["dist/migration/*{.ts,.js}"], "synchronize": false }Pro tip: Keep a copy of the working config in/home/username/backups/so you can revert instantly. -
Step 3 – Set Correct File Permissions
Shared VPSes run under a limited user (often
ftpuser). If your app tries to read.envor write logs, permissions will block it.# Make the app owner your SSH user chown -R $USER:$USER /home/$USER/my-nest-app # Give read/write to config & env files only chmod 640 .env chmod 644 ormconfig.json # Secure the logs folder chmod 750 logs chmod g+s logsWarning: Never set777on production files – it opens a backdoor for attackers. -
Step 4 – Re‑install Node Modules in Production Mode
Development dependencies (like
@nestjs/cli) inflate the bundle and can cause mismatched native binaries.rm -rf node_modules npm ci --only=productionThis guarantees a clean
node_modulestree that matches the exact versions inpackage-lock.json. -
Step 5 – Build the Project with the Right Path
Shared servers often have a different working directory. Use absolute paths in your
npm run start:prodscript."scripts": { "build": "nest build", "start:prod": "node $(pwd)/dist/main.js" }Now
npm run start:prodwill always point to the compiled file, no matter where you launch it from. -
Step 6 – Use a Process Manager (PM2) with Proper Permissions
PM2 keeps your app alive, restarts on crash, and writes logs to a location you control.
npm install -g pm2 pm2 start dist/main.js --name my-nest-api --output /home/$USER/logs/out.log --error /home/$USER/logs/err.log pm2 save pm2 startup systemd -u $USER --hp /home/$USERMake sure the
logsfolder exists and is writable (see Step 3). -
Step 7 – Test, Debug, and Document
Run the app in the background and tail the logs:
pm2 logs my-nest-api --lines 100If you still see
ENOENTorEACCES, double‑check the exact file path printed in the log and compare it to the path on the server.Finally, write a short
README_DEPLOY.mdthat lists:- Node version
- Required environment variables
- Permission commands
- PM2 restart command
Real‑World Use Case: Deploying a SaaS Billing API
I recently applied these exact steps for a SaaS startup that needed a low‑cost billing micro‑service. The VPS was a $4.95 shared plan, the database ran on a managed PostgreSQL instance, and the deadline was 48 hours. After following the 7‑step checklist, the API went from “service unavailable” to handling 500+ requests per minute without a single crash.
Results / Outcome
- Uptime jumped from 30 % to 99.9 % in the first 24 h.
- Server logs stayed clean – no more
ENOENTor permission errors. - Client billed the first month’s invoice two days early, boosting cash flow.
Bonus Tips to Keep Your NestJS VPS Happy
- Use a .env validator. Packages like
dotenv-safeabort startup if a variable is missing, saving you from obscure runtime errors. - Enable swap. Shared plans often have limited RAM; a 512 MB swap file prevents out‑of‑memory kills.
- Schedule log rotation. Add a cron job that runs
logrotateevery week to avoid disk‑full crashes. - Monitor with UptimeRobot. A free 5‑minute check will email you before the client notices downtime.
Monetization Sidebar (Optional)
If you’re a freelancer, turn this checklist into a paid “Deploy‑Ready NestJS Pack” – include a Dockerfile, pre‑configured PM2 scripts, and a one‑click installer. Clients love a $199 turn‑key solution that eliminates hidden server costs.
Fixing the “NestJS not running on shared VPS” nightmare isn’t magical – it’s a series of disciplined steps. Follow the checklist, keep your config tidy, and never ignore permissions again. Your next deployment will be smoother, faster, and profitable.
No comments:
Post a Comment