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

MERN栈项目部署Heroku出现pre-receive hook declined推送失败问题

问题描述

! [remote rejected] main -> main (pre-receive hook declined) error: failed to push some refs to 'https://git.heroku.com/young-lake-89044.git'
我按照对应教程完全同步操作,却触发了上述报错。
我的操作步骤:

  • 执行git remote检查远端仓库是否存在
  • 执行git add .
  • 执行git push heroku
应用信息

我使用MERN技术栈,项目目录结构如下:

Project
 - frontend
 - backend

后端存放服务端脚本,我的项目中该脚本命名为app.js。
/backend路径下的app.js内容如下:

// app.js
const express = require('express');
const connectDB = require('./config/db');
const path = require('path');
var cors = require('cors');

// routes
const articles = require('./routes/api/articles');

const app = express();

// Connect Database
connectDB();

// cors
app.use(cors({ origin: true, credentials: true }));

// Init Middleware
app.use(express.json({ extended: false }));

//app.get('/', (req, res) => res.send('Hello world!'));

// use Routes
app.use('/api/articles', articles);

const port = process.env.PORT || 5000;

if (process.env.NODE_ENV === 'production') {
    app.use(express.static(path.join(__dirname, '../', 'frontend', 'build')));

    app.get('*', (req, res) => {
        res.sendFile(path.join(__dirname, '../', 'frontend', 'build', 'index.html'));
    })
} else {
    app.get('/', (req, res) => {
        res.send('API Running');
    });
}

app.listen(port, () => console.log(`Server running on port ${port}`));

带heroku-postbuild脚本的package.json内容如下:

{
  "name": "mern_a_to_z",
  "version": "1.0.0",
  "description": "",
  "main": "app.js",
  "scripts": {
    "start": "node app.js",
    "app": "nodemon app.js",
    "test": "echo \"Error: no test specified\" && exit 1",
    "heroku-postbuild": "NPM_CONFIG_PRODUCTION=false npm install --prefix ../frontend&&npm run build --prefix frontend"
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/nurislam03/MERN_A_to_Z.git"
  },
  "author": "Nur Islam",
  "license": "MIT",
  "bugs": {
    "url": "https://github.com/nurislam03/MERN_A_to_Z/issues"
  },
  "homepage": "https://github.com/nurislam03/MERN_A_to_Z#readme",
  "dependencies": {
    "bcryptjs": "^2.4.3",
    "body-parser": "^1.19.0",
    "config": "^3.3.6",
    "cors": "^2.8.5",
    "dotenv": "^10.0.0",
    "express": "^4.17.1",
    "mongodb": "^3.6.11",
    "mongoose": "^5.5.15",
    "validation": "0.0.1"
  },
  "devDependencies": {
    "nodemon": "^1.19.1"
  }
}

存放在backend目录下的Procfile内容如下:

web: node app.js
完整错误输出
PS C:\Users\User\Documents\GitHub\CISE-Team-1> git push heroku
Enumerating objects: 32838, done.
Counting objects: 100% (32838/32838), done.
Delta compression using up to 8 threads
Compressing objects: 100% (25047/25047), done.
Writing objects: 100% (32838/32838), 37.24 MiB | 48.00 KiB/s, done.
Total 32838 (delta > 6984), reused 30707 (delta 6475)
remote: Compressing source files... done.
remote: Building source:
remote:
remote: -----> Building on the Heroku-20 stack
remote: -----> Determining which buildpack to use for this app
remote:  !     No default language could be detected for this app.
remote:                         HINT: This occurs when Heroku
                                        cannot detect the buildpack to use
                                        for this application automatically.
remote:
remote:  !   Push failed
remote:  !
remote:  ! ## Warning - The same version of this code
has already been built: a5ffe033316042c0575cd8e67d548460f8607950
remote:  !
remote:  ! We have detected that you have triggered a build from
           source code with version 5ffe033316042c0575cd8e67d548460f8607950
remote:  ! at least twice. One common cause of this behavior
           is attempting to deploy code from a different branch.
remote:  !
remote:  ! If you are developing on a branch and deploying
           via git you must run:
remote:  !
remote:  !    git push heroku <branchname>:main
remote:  !
remote: Verifying deploy...
remote:
remote: !       Push rejected to young-lake-89044.
remote: To https://git.heroku.com/young-lake-89044.git
 ! [remote rejected]  main -> main (pre-receive hook declined)
error: failed to push some refs to 'https://git.heroku.com/young-lake-89044.git'

首次执行push命令时没有出现同版本代码重复构建的提示,且该应用在本地环境运行完全正常。

已尝试的解决方案
  • git push heroku master
  • git push heroku main
  • git push heroku
  • 删除backend目录下的package-lock.json
  • 重新执行git init
  • git add .
  • git commit -m "First commit"
  • heroku create
  • git push heroku master
  • git push heroku main:master
  • 执行git commit -m dummy --allow-empty提交空提交
修复方案

本次报错的核心原因是No default language could be detected for this app,Heroku默认会检测项目根目录的配置文件识别项目类型,你当前把package.json、Procfile都放在了backend子目录下,根目录没有Node.js项目的标识文件,所以无法正常构建。按照以下步骤操作即可修复:

  1. 把backend目录下的package.json、package-lock.json、Procfile三个文件移动到项目根目录(和frontend、backend文件夹同级的位置)
  2. 修改根目录下package.json的三个配置项:
    • 将main字段从app.js修改为backend/app.js
    • 将start脚本从node app.js修改为node backend/app.js
    • 将heroku-postbuild脚本修改为NPM_CONFIG_PRODUCTION=false npm install --prefix frontend && npm run build --prefix frontend
  3. 手动给Heroku应用指定Node.js构建包,执行命令:heroku buildpacks:set heroku/nodejs
  4. 重新提交代码并推送:
git add .
git commit -m "adjust config for heroku deployment"
git push heroku main

内容的提问来源于stack exchange,提问作者user11518995

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.04 09:00:02