DEV Community

Cover image for A Beginner's Guide to Running a MERN Stack Application Locally
Rohitash Singh
Rohitash Singh

Posted on

A Beginner's Guide to Running a MERN Stack Application Locally

Running a MERN (MongoDB, Express.js, React.js, Node.js) stack application locally is a crucial step in the development process. This guide aims to walk beginners through the steps required to set up and run a MERN application on their local machine. By following these steps, you'll have a fully functional MERN application running locally, ready for development or testing.

1. Install Required Software:
Before getting started, ensure that you have the following software installed on your machine:

Node.js: Visit the official Node.js website (https://nodejs.org) and download the latest version for your operating system. Follow the installation instructions provided.
Note: In this tutorial, I am using a MongoDB online URI.

2. Set Up MongoDB Atlas:

  • MongoDB Atlas is a cloud-based database service that allows you to set up, manage, and scale MongoDB databases easily.
  • Sign up for a free account on the MongoDB Atlas website (https://www.mongodb.com/cloud/atlas).
  • Create a new cluster, choosing the free tier option.
  • Once your cluster is created, click on the "Connect" button and choose "Connect Your Application".
  • Copy the connection string provided, which includes your MongoDB username, password, and cluster address.

Paste the connection string into .env file with following name MONGODB_CONNECT_STRING.

**3. Create a New React Project:

  • Open a terminal window and navigate to the directory where you want to create your MERN project.
  • Run the following command to create a new React App using Create React App:
npx create-react-app my-react-app
Enter fullscreen mode Exit fullscreen mode
  • Once the project is created, navigate into the project directory:
cd my-react-app
Enter fullscreen mode Exit fullscreen mode

4. Set Up Express.js Backend:

  • In your project directory, create a new directory named server to hold your Express.js backend code:
mkdir server
Enter fullscreen mode Exit fullscreen mode
  • Navigate into the server directory:
cd server
Enter fullscreen mode Exit fullscreen mode
  • Initialize a new Node.js project by running:
npm init -y
Enter fullscreen mode Exit fullscreen mode
  • Install Express.js and Mongoose (MongoDB object modeling tool) as dependencies:
npm install express mongoose
Enter fullscreen mode Exit fullscreen mode
  • Create a new file named server.js and write your index.js backend code in this file. Use the MongoDB Atlas connection string to connect to your database.

5. Connect React Frontend with Express Backend:

  • In the client directory of your React project, update the package.json file to include a proxy to your Express backend. Add the following line:
"proxy": "http://localhost:5000"
Enter fullscreen mode Exit fullscreen mode
  • This configuration allows your React frontend to communicate with the Express backend.

**6. Start the Development Servers:"

  • Open two terminal windows, one for the client and one for the server.
  • In the first terminal, navigate to the client directory of your React project and start the React development server:
cd my-react-app/client
npm start
Enter fullscreen mode Exit fullscreen mode
  • In the second terminal, navigate to the server directory and start the Express.js server:
cd my-react-app/server
node index.js
Enter fullscreen mode Exit fullscreen mode

7. Test Your Application:

  • Open a web browser and navigate to http://localhost:3000 to view your React frontend.
  • Test the functionality of your application to ensure that the frontend and backend are communicating correctly.

Congratulations! You've successfully set up and run a MERN stack application locally on your machine, utilizing MongoDB Atlas as the cloud-based database solution. This guide has provided you with the essential steps to get started with MERN development. From here, you can further customize and expand your application based on your project requirements.

Happy coding! 🚀

Top comments (0)