How to Deploy NestJS on Namecheap Shared Hosting
Deploying NestJS to Namecheap shared hosting is not like deploying a static site or a PHP app. You cannot just upload files and expect it to work.
You must compile TypeScript to JavaScript, install production dependencies, and use cPanel to manage the process via Passenger.
I faced empty folders, missing build files, and broken Redis connections before I found the right way. Here is the proven workflow.
The Setup Logic The request follows this path: • Browser or API client • Domain and HTTPS • Apache / Passenger on Namecheap • app.js (the bridge) • dist/main.js (the compiled code) • NestJS application
Key Requirements • A Namecheap hosting account with Node.js support • Access to cPanel and SSH/Terminal • A working NestJS project • Production credentials for your database or Redis
Step 1: Prepare your code Do not use a hard-coded port. In your src/main.ts, use process.env.PORT:
async function bootstrap() { const app = await NestFactory.create(AppModule); await app.listen(process.env.PORT || 4000); }
Step 2: Organize dependencies Move runtime packages like @nestjs/common and @nestjs/core to the dependencies section in package.json. If they stay in devDependencies, your app will crash in production.
Step 3: Build and Create the Bridge Namecheap runs JavaScript, not TypeScript. • Run npm run build locally. • Verify dist/main.js exists. • Create an app.js file in your project root with this line: require('./dist/main');
This app.js acts as the entry point for Passenger.
Step 4: Deployment Strategy The safest way is to build locally and upload the artifacts: • Upload app.js • Upload dist/ folder • Upload package.json and package-lock.json • On Namecheap, run: npm ci --omit=dev
Never upload your local node_modules folder. Native modules built on your computer will not work on a Linux server.
Step 5: Configure cPanel Go to Setup Node.js App in cPanel: • Select the correct Node.js version. • Set mode to Production. • Set Application root to your folder. • Set Application startup file to app.js.
Step 6: Environment Variables Add your secrets (like DATABASE_URL or API keys) directly in the cPanel Node.js interface. You can also use a .env file in the root, but ensure it is not inside the dist folder. Restart the app after making changes.
Common Troubleshooting • EADDRINUSE: You likely have a manual process running. Let Passenger manage the app. • Cannot find module: Your build is missing or the path in app.js is wrong. • Redis connection failed: Check if Namecheap allows outbound connections on your Redis port. You might need to ask Namecheap support to open it.
Source: https://dev.to/adesoji/how-to-deploy-and-run-a-nestjs-app-on-namecheap-shared-hosting-5c45
