The MEAN stack is one of the most popular tech stacks used for developing web applications. It includes MongoDB, Express.js, Angular, and Node.js. This tutorial will walk you through the steps to set up the MEAN stack on your local machine, regardless of whether you're using Windows, Linux, or macOS.
Prerequisites
Before we begin, make sure that you have the following:
- A computer with one of the supported operating systems (Windows, Linux, or macOS).
- An internet connection to download the required tools.
Step 1: Install Node.js and NPM
The Node.js runtime and the Node Package Manager (NPM) are essential for running both the server-side (Node) and the client-side (Angular) code.
Windows
- Go to the Node.js download page.
- Download the LTS version (Long Term Support).
- Run the installer and follow the on-screen instructions.
- To check if Node.js is installed correctly, open the Command Prompt and type:
Linux
- Open a terminal window.
- For most Linux distributions, use the following command to install Node.js and NPM:
- To verify the installation, check the version of Node.js and NPM:
macOS
- Open the Terminal.
- Install Node.js using Homebrew (if you don’t have Homebrew installed, you can find installation instructions here):
- After installation, verify the installation:
Step 2: Install MongoDB
MongoDB is the database used in the MEAN stack. It stores data in a flexible, JSON-like format.
Windows
- Go to the MongoDB download page.
- Download the MSI installer for Windows.
- Run the installer and follow the installation steps.
- By default, MongoDB is installed in
C:\Program Files\MongoDB\Server\5.x\bin
. - Start MongoDB by opening the Command Prompt and typing:
- Open another Command Prompt window and type:
This should connect you to the MongoDB shell.
Linux
- Open a terminal window and update your package list:
- Install MongoDB:
- Start MongoDB:
- Check the status of MongoDB to ensure it’s running:
macOS
- Install MongoDB using Homebrew:
- Start MongoDB:
Step 3: Install Angular CLI
Angular is the front-end framework in the MEAN stack. To set up Angular, you need to install the Angular CLI.
Windows, Linux, and macOS
- Open a terminal/command prompt and install the Angular CLI globally using NPM:
- Verify the installation:
Step 4: Create Your First MEAN Stack Application
Now that all the dependencies are installed, it’s time to create a simple MEAN stack application.
1. Set Up the Backend (Node.js + Express)
- Open your terminal/command prompt and create a new directory for your project:
- Initialize a Node.js project:
- Install the required dependencies:
- Create a file named
server.js
inside your project folder. Here's a simple Express server setup:
2. Set Up the Frontend (Angular)
- Create a new Angular application:
- Navigate into the Angular application directory:
- To serve the Angular app, run:
This will start the Angular development server and you can access your application at
http://localhost:4200/
.
Step 5: Test Your Application
- Start your backend server (Node + Express):
- In another terminal window, start the Angular frontend:
- Open your browser and go to
http://localhost:4200/
to see your Angular app running. The backend should be available athttp://localhost:3000/
.
Step 6: Next Steps
- Add more routes to your Express server to handle different requests.
- Create MongoDB models to interact with the database.
- Connect the front-end with the back-end by using Angular services to make HTTP requests to the Express API.
Conclusion
In this tutorial, you’ve learned how to set up a local environment for MEAN stack development on Windows, Linux, and macOS. You’ve also created a basic backend with Express.js, set up a MongoDB database, and generated an Angular frontend.
With the basic setup in place, you can now start building more complex applications using the full power of the MEAN stack. Happy coding!