MERN Stack Deployment Guide: Hosting Frontend on Vercel/Netlify & Backend on Render

Deploying full-stack applications often presents a unique challenge, especially when aiming for optimal performance and cost-effectiveness. This guide tackles a common scenario: hosting your frontend on a platform like Vercel or Netlify and your backend on Render.com, even when both reside in the same GitHub repository. Discover the steps, insights, and successful strategies for uniting these disparate parts under different hosting platforms.

MERN Stack Deployment Architecture

Hosting Your Frontend: Vercel or Netlify

For the frontend of your MERN stack application, Vercel and Netlify are excellent choices, known for their speed, developer-friendliness, and robust CDNs.

Step-by-Step Frontend Deployment:

  1. Select Your Repository: Begin by logging into your Vercel or Netlify account. Connect your GitHub (or preferred Git provider) and select the repository containing your MERN stack project.

Select Repository on Vercel/Netlify

  1. Specify Root Directory: Click on "Root Directory" and select your frontend folder from the list of folders in your repository. This tells the hosting platform where your frontend application's code is located.

Choose Frontend Folder in Root Directory

  1. Add Environment Variables & Deploy: Input any necessary environment variables specific to your frontend (e.g., API URLs). Once configured, click the "Deploy" button to initiate the deployment process.

Add Environment Variables and Deploy Frontend

  1. Frontend Deployed! 🎉 Your frontend application should now be successfully deployed and accessible via the provided URL.

Frontend Deployment Success

Hosting Your Backend: Render.com

Render.com is an excellent choice for hosting Node.js backends due to its ease of use, automatic deployments, and support for various services.

Step-by-Step Backend Deployment:

  1. Create a New Web Service: On Render.com, navigate to your dashboard and select "New Web Service" to start a new Node.js deployment.

New Web Service on Render.com

  1. Build and Deploy from Git: Choose the option to "Build and deploy from a Git repository."

Build and Deploy from Git

  1. Connect Git Account: Connect your GitHub or GitLab account to allow Render to access your repositories.
  1. Select Your Repository: From your connected Git repositories, select the one that contains your MERN stack project.

Select Git Repository

  1. Specify Root Directory (Backend): In the "Root Directory" field, accurately type the name of your backend folder. This ensures Render builds and deploys the correct part of your monorepo.

Enter Backend Folder Name in Root Directory

  1. Enter Start Command & Access Advanced Settings: Scroll down and enter your backend's **start command** (e.g., `npm start` or `node index.js`). Then, click on "Advanced" to configure environment variables.

Start Command and Advanced Settings

  1. Add Environment Variables: In the Advanced settings, add all necessary environment variables for your backend (e.g., database connection strings, API keys).
  1. Create New Service: Finally, click on "Create New Service."

Render will now take some time to build and deploy your backend service.

Backend Deployment Progress

Hurray! 🎉 Your MERN stack project has been successfully hosted with its frontend and backend living on their respective optimal platforms!

Did you find this guide helpful? Let me know if you have any questions or further deployment scenarios you'd like to explore!

Previous Post Next Post