You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

React.js+Node.js+MongoDB项目部署Heroku执行git push时报错求助

Fixing the "Remote Rejected" Error When Deploying React/Node/MongoDB App to Heroku

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 Procfile in your project root (no file extension!) with this line:
    web: node server.js
    
    Replace server.js with your actual backend entry file (like index.js if 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 build locally 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_URI environment variable with your actual MongoDB connection string (like from MongoDB Atlas).
  • Double-check your code uses process.env.MONGODB_URI instead 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 (or yarn build if 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.json and package-lock.json (or yarn.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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.28 14:02:32