Deploying a MERN App Using Render and Vercel: A Developer's Journey

Ah, deployment—the phase of development that can often feel like a mysterious art rather than a science. If you've ever found yourself wrestling with server configurations or tangled up in environment variables, you're not alone. Just recently, I embarked on a deployment adventure with my MERN stack application, and let me tell you, it was a rollercoaster ride.

After a day full of trial and error (and a fair share of caffeine), I finally cracked the code. So, if you're ready to deploy your MERN app using Render and Vercel, let's dive in and hopefully make your journey a bit smoother than mine!

Step 1: Preparing Your Code

Before we even think about deployment, there’s some housekeeping to do. Ensure your code is safely tucked away in a GitHub repository. But wait—there's more. Here's what you should double-check:

  1. Port Configuration:

Inside your index.js or server.js file, add this crucial line if it’s not already there:

const PORT = process.env.PORT || 4000;

This seemingly small line is a lifesaver. It ensures your app listens on the correct port, which is especially important when deploying on platforms like Render. Trust me, skipping this step will send you headfirst into a bug-fixing rabbit hole.

Step 2: Deploying the Backend with Render

Now, let's talk about Render. It's like the Swiss Army knife of hosting platforms for your Node.js applications. Here's how you can get your backend up and running:

  1. Sign In to Render:

Head over to Render.com and sign in. If you don’t have an account yet, creating one is straightforward.

  1. Create a New Web Service:

    Click on the New icon, then choose Web Service from the menu. This is where the magic begins.

  2. Connect Your GitHub Repository:

    Render will ask you to select a GitHub repository. Choose the one that houses your backend code.

  3. Configure Build and Start Commands:

    Here's where you tell Render how to get your backend up and running:

    cd backend && npm install

    This command tells Render to navigate to your backend directory and install the necessary dependencies.

  4. Start Command: Similarly, the start command should be

    cd backend && npm start

    This command kicks off your backend application.

  5. Set Environment Variables:

    Under the Environment Variables section, add any necessary environment variables from your .env file, like database URLs or API keys. These are crucial for your app to function properly in a production environment.

  6. Deploy Your Service:

    Click Deploy Web Service. Take a deep breath, grab a cup of coffee, and wait a few minutes. Once deployed, you'll receive a shiny new URL for your backend. Make sure to copy this URL—you'll need it soon!

    Step 3: Deploying the Frontend with Vercel

    Now, let's shift gears and deploy the frontend. Vercel is our go-to platform here, known for its seamless integration with modern frontend frameworks.

    1.Sign In to Vercel:

    Go to Vercel.com and sign in. Like with Render, setting up an account is quick and easy if you haven’t done so yet.

    1. Add a New Project:

      Click on Add New, and select the GitHub repository where your frontend code resides.

    2. Framework Detection:

      Vercel is pretty smart and usually detects the framework you’ve used (e.g., React). If it doesn’t, you can manually specify it.

    3. Root Directory Selection:

      If your frontend code is inside a folder like frontend, point Vercel to that directory. This tells Vercel exactly where to find the code it needs to deploy.

    4. Update Backend URL:

      This is crucial: make sure to update your frontend code to use the newly deployed backend URL instead of localhost. Otherwise, your frontend won't be able to communicate with your backend once it's live.

    5. Deploy Your Project:

      Click Deploy. Vercel will take care of the rest, and in just a few moments, your frontend will be up and running for the world to see.

    6. Conclusion

      And there you have it! Your full-stack MERN app is now live, ready for you to share with friends, family, and the rest of the world. Deployment can be daunting, but with platforms like Render and Vercel, you can navigate the process with ease.

      I hope this guide has helped demystify the deployment process and added some value to your development journey. If you’ve got any questions, feedback, or just want to share your own deployment stories, feel free to reach out. Thanks for reading, and happy coding!

12
Subscribe to my newsletter

Read articles from Ashish Nagvanshi directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Ashish Nagvanshi
Ashish Nagvanshi

Currently learning fullstack stack development