What is the MERN Stack?
The MERN stack consists of the following technologies:
- MongoDB - A NoSQL database that stores data in flexible, JSON-like documents.
- Express.js - A lightweight web framework for Node.js to build backend APIs.
- React - A powerful JavaScript library for building user interfaces, primarily used for frontend development.
- Node.js - A runtime environment that allows JavaScript to be run on the server side.
Step 1: Install Prerequisites
Before setting up the MERN stack, you need to install a few key tools. The following tools are needed for all operating systems:
- Node.js (Includes npm - Node Package Manager)
- MongoDB
- Git (Optional but recommended)
A. Installing Node.js
Node.js is the heart of the MERN stack and is essential for both frontend and backend development. Here's how to install it on your system:
On Windows:
- Go to Node.js official website and download the Windows installer.
- Run the installer and follow the setup instructions. Ensure you check the option to install npm (Node Package Manager) during the installation process.
- After installation, open Command Prompt and verify installation by typing:
node -v
npm -v
On macOS:
- You can install Node.js using Homebrew, a package manager for macOS. First, make sure Homebrew is installed by running:
- Once Homebrew is installed, run the following command to install Node.js:
- Verify the installation by typing:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
brew install node
node -v
npm -v
On Linux:
- Open your terminal and update your package index:
- Install Node.js by running:
- Verify the installation:
sudo apt update
sudo apt install nodejs npm
node -v
npm -v
B. Installing MongoDB
MongoDB is a NoSQL database used in MERN stack applications to store and retrieve data.
On Windows:
- Go to the MongoDB download page and download the latest stable release for Windows.
- Run the installer and follow the prompts. Choose the "Complete" installation type.
- Once installed, open Command Prompt and start MongoDB by running:
mongod
MongoDB will now run in the background.
On macOS:
- Open your terminal and install MongoDB using Homebrew:
- After installation, start MongoDB with the following command:
brew tap mongodb/brew
brew install mongodb-community
brew services start mongodb/brew/mongodb-community
On Linux:
- Install MongoDB by running:
- Start MongoDB using the following command:
- Enable MongoDB to start automatically on boot:
sudo apt install -y mongodb
sudo systemctl start mongodb
sudo systemctl enable mongodb
Step 2: Install Git (Optional but Recommended)
Git is a version control system that allows you to manage your codebase. While not strictly necessary, it’s highly recommended for collaborative projects and version control.
On Windows:
- Go to the Git download page and download the installer.
- Follow the setup instructions and make sure to select the option to add Git to your system’s PATH during the installation process.
On macOS and Linux:
Git is typically pre-installed on these systems. To check if Git is already installed, run:
git --version
If it’s not installed, you can install it using Homebrew on macOS:
brew install git
And on Linux:
sudo apt install git
Step 3: Install the MERN Stack Dependencies
Now that you have the necessary tools installed, let's install the libraries and frameworks for the MERN stack.
A. Installing Express.js (Backend)
Express.js is a minimal and flexible Node.js web application framework that is crucial for backend development.
- 1. Open your terminal or command prompt and create a new directory for your project:
- Initialize a new Node.js project:
- Install Express.js:
mkdir mern-project
cd mern-project
npm init -y
This will create a package.json file that will manage your project dependencies.
npm install express
B. Installing React (Frontend)
React is a powerful library for building user interfaces.
- Create a new React application inside your project directory:
- This will automatically install React and create a directory named client with a default React app structure.
npx create-react-app client
C. Install MongoDB Driver
MongoDB is essential for data storage, and you need to connect it to your Node.js server. Install the MongoDB driver with the following command:
npm install mongodb
Step 4: Create a Simple Backend API
- Inside the mern-project directory, create an index.js file. This will be the entry point for your backend server.
// index.js const express = require('express'); const app = express(); const port = 5000; app.get('/', (req, res) => { res.send('Hello from the backend!'); }); app.listen(port, () => { console.log(`Server running on http://localhost:${port}`); });
- Run the server:
node index.js
- Open your browser and go to http://localhost:5000. You should see "Hello from the backend!" displayed.
Step 5: Create a Simple React Frontend
- Go to the client directory:
cd client
- Open the src/App.js file and replace the content with:
import React from 'react'; function App() { return (
Hello from React!
- Start the React development server:
npm start
- Open your browser and go to http://localhost:3000. You should see "Hello from React!" displayed.
Step 6: Connecting the Frontend and Backend
To make your MERN app fully functional, you'll need to connect the frontend (React) with the backend (Express). This involves making API calls from React to Express.
- In client/src/App.js, replace the content with:
import React, { useEffect, useState } from 'react'; function App() { const [message, setMessage] = useState(''); useEffect(() => { fetch('http://localhost:5000') .then((response) => response.text()) .then((data) => setMessage(data)); }, []); return (
{message}
- Restart both the React and Express servers:
- In the React client folder:
npm start
- In the main mern-project folder:
node index.js
- In the React client folder:
Now, when you refresh the React app, it will fetch the message from the Express backend.
Conclusion
Congratulations! You've just set up a local MERN stack environment on your system. You now have the tools to build powerful web applications with MongoDB, Express.js, React, and Node.js.
Happy coding, and good luck building your MERN applications! Feel free to explore more advanced topics like authentication, state management, and deployment as you progress.