React.js+Node.js+MongoDB项目部署Heroku执行git push时报错求助
Hey there, sorry to hear you're stuck on deploying your app to Heroku! That remote rejected error from the pre-receive hook usually traces back to issues with your app's configuration, dependencies, or build workflow. Let's break down the most common fixes to get your app live:
1. Double-Check Your Procfile
Heroku relies on a Procfile to know how to start your application. For a Node.js backend + React frontend setup:
- Create or edit the
Procfilein your project root (no file extension!) with this line:
Replaceweb: node server.jsserver.jswith your actual backend entry file (likeindex.jsif that's what you use). - If you're deploying both frontend and backend in one Heroku app, make sure your backend is set up to serve the React static build files (after running
npm run buildlocally first).
2. Specify Compatible Node.js Versions
Heroku uses the Node.js version defined in your package.json—if it's missing or incompatible, builds fail. Add or update the engines field:
"engines": { "node": "18.x", "npm": "9.x" }
Use versions close to what you're running locally to avoid unexpected compatibility bugs.
3. Verify MongoDB Connection Configuration
Since your app uses MongoDB, you need to make sure Heroku has the right environment variables:
- Head to your Heroku app's Settings page, add a
MONGODB_URIenvironment variable with your actual MongoDB connection string (like from MongoDB Atlas). - Double-check your code uses
process.env.MONGODB_URIinstead of a hardcoded local connection string.
4. Ensure Frontend Builds Successfully Locally
If your React build fails, Heroku will reject the push. Test this first locally:
- For Create React App projects, run
npm run build(oryarn buildif you use Yarn). - Fix any build errors you see before trying to push to Heroku again.
5. Get Detailed Error Logs
The short error message you got doesn't tell the whole story. Run this command to see full, real-time logs:
heroku logs --tail
This will show you exactly where the build or deployment is failing—whether it's a missing dependency, a syntax error, or something else.
6. Check Your .gitignore File
Make sure:
- You're not committing
node_modules/(it should be in.gitignore—Heroku installs dependencies itself). - You are including
package.jsonandpackage-lock.json(oryarn.lock)—Heroku needs these to install dependencies.
If you try these steps and still hit issues, share the specific error lines from heroku logs --tail and we can dig deeper!
内容的提问来源于stack exchange,提问作者Yassin Mo

