DEV Community

Cover image for Deploying Your MERN Stack Application on Render: A Step-by-Step Guide
Rehman Khan
Rehman Khan

Posted on

Deploying Your MERN Stack Application on Render: A Step-by-Step Guide

Hello, Dev Community! πŸ™Œ

Deploying a MERN (MongoDB, Express.js, React, Node.js) stack application can be challenging, especially when handling both the frontend and backend. In this guide, I’ll walk you through deploying a MERN stack application using Render, with a clear separation between the client (React) and server (Node.js/Express) directories.

Let’s dive into the deployment process while also highlighting the importance of managing your .gitignore and .env files.

πŸ“ Project Structure Overview

Here’s how our project is structured:

project-root/
β”‚
β”œβ”€β”€ client/   # Contains React code
β”‚   β”œβ”€β”€ public/
β”‚   β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ package.json
β”‚
β”œβ”€β”€ server/   # Contains Node.js/Express.js code
β”‚   β”œβ”€β”€ controllers/
β”‚   β”œβ”€β”€ routes/
β”‚   β”œβ”€β”€ utils/
β”‚   β”œβ”€β”€ server.js
β”‚   β”œβ”€β”€ package.json
β”‚   β”œβ”€β”€ .env
β”‚
β”œβ”€β”€ .gitignore
└── README.md"
Enter fullscreen mode Exit fullscreen mode

πŸ“¦ Step 1: Prepare Your Application for Deployment

1.1 Configure server/package.json

In your server/package.json, add the following scripts to handle both the frontend and backend builds:

"scripts": {
    "start": "node server.js",
    "frontend-build": "cd ../client && npm install && npm run build",
    "backend-build": "npm install",
    "build": "npm run frontend-build && npm run backend-build",
    "dev":   "nodemon server.js"
}
Enter fullscreen mode Exit fullscreen mode
  • frontend-build: Navigates to the client directory, installs dependencies, and builds the React app.
  • backend-build: Installs dependencies for the server.
  • build: Runs both frontend-build and backend-build.

1.2 The Importance of the .gitignore File

The .gitignore file is crucial in any project as it ensures that sensitive or unnecessary files don’t get pushed to your Git repository. This typically includes:

  • Node modules: Large dependency files that should be installed via npm install rather than stored in your repository.
  • Build files: You don't need to store generated files like build/ from React.
  • Environment files: Keep sensitive credentials and keys out of your version control.

Here’s a basic example of what your .gitignore file might look like:

/node_modules
/client/build
/server/node_modules
/server/.env
Enter fullscreen mode Exit fullscreen mode

1.3 The Role of the .env File

The .env file is where you store environment variables, such as API keys, database URIs, and secret tokens. This file should never be committed to your Git repository for security reasons. Instead, it should be added to your .gitignore file to ensure it remains local.

Example .env file for your server:

MONGODB_URI=your_mongo_db_uri
JWT_SECRET=your_jwt_secret
PORT=8080
PASSWORD=your_app_password_for_email
EMAIL=your_gmail_email
BACKEND_URL=your_backend_url/api/v1
FRONTEND_URL=your_frontend_url
PRODUCTION=true
Enter fullscreen mode Exit fullscreen mode

🌐 Step 2: Deploying to Render

2.1 Push Your Code to GitHub

First, ensure that your code is pushed to a GitHub repository. This will allow Render to pull your code during the deployment process.

Step 1: Create a GitHub Repository

  • Go to GitHub and log in to your account.
  • Click on the β€˜+’ icon in the top right corner and select β€˜New repository.’
  • Give your repository a name, and optionally add a description.
  • Choose the repository to be either public or private.
  • Click β€˜Create repository.’

Step 2: Initialize Git in Your Project

If you haven’t already initialized a Git repository in your project, do so by running the following command in your project root directory:

   git init
Enter fullscreen mode Exit fullscreen mode

Step 3: Add Your Files to the Repository

Add all your project files to the repository using the following command:

    git add .
Enter fullscreen mode Exit fullscreen mode

Step 4: Commit Your Changes

Commit the added files with a descriptive commit message:

    git commit -m 'Initial commit'
Enter fullscreen mode Exit fullscreen mode

Step 5: Add the GitHub Repository as a Remote

Link your local repository to the GitHub repository you just created:

    git remote add origin https://github.com/your-username/your-repository-name.git
Enter fullscreen mode Exit fullscreen mode

Replace your-username and your-repository-name with your GitHub username and the name of your repository.

Step 6: Push Your Code to GitHub

Finally, push your code to the GitHub repository using the following command:

   git push --set-upstream origin master
Enter fullscreen mode Exit fullscreen mode

This command will push your code to the main branch of your GitHub repository.

Now your code is hosted on GitHub, and you can proceed with the deployment on Render!

2.2 Create a New Web Service on Render

  • Log in to your Render account (or sign up if you don’t have one).
  • Click on β€œNew” and select β€œWeb Service.”

New Webservice

  • Connect your GitHub repository containing your MERN stack project.
  • Select the server directory when prompted for the root directory.

Project Specifications

2.3 Configure Environment Variables on Render

Navigate to the β€œEnvironment” section in your Render dashboard and add the environment variables from your .env file. This ensures that your application has access to the necessary secrets and API keys in the production environment.

2.4 Build and Deploy

Render will automatically detect your package.json scripts and begin the build process. It will run the build script, which triggers both the frontend and backend builds.

Once the build is complete, your application will be deployed and accessible via the URL provided by Render.

πŸŽ‰ Conclusion

Deploying a MERN stack application on Render is a straightforward process when you have a well-structured project and a clear understanding of your environment configurations. By following these steps, you can ensure a smooth deployment experience, while keeping your sensitive data secure with proper .gitignore and .env management.

Happy coding! πŸš€

Top comments (2)

Collapse
 
pranav_rao_14fb8d49136bb3 profile image
Pranav Rao

So the frontend deploys on Render and the server starts running after deployment, but whenever I try to do any of the CRUD operations like updating password, logging in, etc. the usual error notifications, going to the main page or returning to login doesn't happen. It's as if the client isn't interacting with the server whatsoever on my end.

Collapse
 
seyed_mohamedbuhari_a3a7 profile image
Seyed Mohamed Buhari

I am facing error in hosting over render.
byoode.onrender.com/
Can you help?