Deploying your MERN stack project for free.
Deploying a MERN (MongoDB, Express, React, Node.js) stack on Heroku involves several steps. Below is a general guide to deploying your MERN application on Heroku:
1. Prepare Your MERN Application:
Ensure MongoDB Connection: Make sure your MongoDB connection string is configured correctly in your backend code.
Update
package.json
Scripts: Update yourpackage.json
file in both the client (React) and server (Node.js) directories to include a"start"
script. This is the command that Heroku will use to start your application.Example for the server
package.json
:jsonCopy code"scripts": { "start": "node server.js", // other scripts... }
Example for the client
package.json
:jsonCopy code"scripts": { "start": "react-scripts start", // other scripts... }
2. Create a Heroku Account:
If you don't have a Heroku account, you'll need to sign up on the Heroku website.
3. Install Heroku CLI:
Download and install the Heroku CLI by following the instructions on the official Heroku CLI page.
4. Login to Heroku:
Open a terminal and log in to your Heroku account:
bashCopy codeheroku login
Follow the prompts to log in.
5. Initialize Git Repository (if not already):
If your project is not already a Git repository, initialize one:
bashCopy codegit init
git add .
git commit -m "Initial commit"
6. Create a Heroku App:
Create a new Heroku app with a unique name:
bashCopy codeheroku create your-unique-app-name
7. Configure Environment Variables:
Set any environment variables that your application requires on Heroku. This might include your MongoDB connection string, API keys, or any other sensitive information.
bashCopy codeheroku config:set MONGODB_URI=<your-mongodb-uri>
8. Configure Buildpacks:
If your project includes both frontend and backend code, you may need to specify multiple buildpacks. For a MERN stack, you'll need both Node.js and create-react-app buildpacks.
bashCopy codeheroku buildpacks:set heroku/nodejs
heroku buildpacks:add --index 1 heroku/create-react-app
9. Configure Heroku Postbuild Script:
In your server package.json
, ensure that you have a heroku-postbuild
script to build the React app during deployment.
jsonCopy code"scripts": {
"start": "node server.js",
"heroku-postbuild": "cd client && npm install && npm run build"
// other scripts...
}
10. Commit Changes and Deploy:
Commit your changes and deploy your application to Heroku:
bashCopy codegit add .
git commit -m "Prepare for Heroku deployment"
git push heroku master
11. Open Your Application:
Once the deployment is complete, you can open your application in the browser:
bashCopy codeheroku open
Additional Tips:
Review the Heroku logs if there are any deployment issues:
bashCopy codeheroku logs --tail
Ensure your backend server is listening on the correct port:
javascriptCopy codeconst PORT = process.env.PORT || 5000; app.listen(PORT, () => { console.log(`Server is running on port ${PORT}`); });
If you have static assets, configure your Express server to serve them in production.