! [远程拒绝] master -> master (pre-receive hook declined) 当试图部署 mern app 到 heroku

! [remote rejected] master -> master (pre-receive hook declined) when trying to deploy mern app to heroku

我是 NodeJs 的初学者,我正在关注 brad traversy 最近发布的 MERN 堆栈 youtube 教程。我已经完成了教程并进入了应用程序的部署部分,但是在按照教程进行操作时出现以下错误。我真的不明白这个问题是从哪里来的,所以我附上了 server.js 文件和 package.json 文件以及终端中的实际 Heroku 错误。

Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 12 threads
Compressing objects: 100% (4/4), done.
Writing objects: 100% (5/5), 21.14 KiB | 10.57 MiB/s, done.
Total 5 (delta 0), reused 0 (delta 0), pack-reused 0
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: -----> Node.js app detected
remote:        
remote: -----> Creating runtime environment
remote:
remote:        NPM_CONFIG_LOGLEVEL=error
remote:        NODE_VERBOSE=false
remote:        NODE_ENV=production
remote:        NODE_MODULES_CACHE=true
remote:        
remote: -----> Installing binaries
remote:        engines.node (package.json):  unspecified
remote:        engines.npm (package.json):   unspecified (use default)
remote:
remote:        Resolving node version 16.x...
remote:        Downloading and installing node 16.14.0...
remote:        Using default npm version: 8.3.1
remote:        
remote: -----> Installing dependencies
remote:        Installing node modules
remote:        
remote:        added 226 packages, and audited 227 packages in 13s
remote:
remote:        24 packages are looking for funding
remote:          run `npm fund` for details
remote:
remote:        found 0 vulnerabilities
remote:        
remote: -----> Build
remote:        Running heroku-postbuild
remote:        
remote:        > mern-stack-2022@1.0.0 heroku-postbuild
remote:        > NPM_CONFIG_PRODUCTION=false npm install --prefix frontend && npm run build --prefix frontend
remote:
remote: npm ERR! code ENOENT
remote: npm ERR! syscall open
remote: npm ERR! path /tmp/build_b752602d/frontend/package.json
remote: npm ERR! errno -2
remote: npm ERR! enoent ENOENT: no such file or directory, open '/tmp/build_b752602d/frontend/package.json'
remote: npm ERR! enoent This is related to npm not being able to find a file.
remote: npm ERR! enoent
remote:
remote: npm ERR! A complete log of this run can be found in:
remote: npm ERR!     /tmp/npmcache.esoMP/_logs/2022-02-22T21_55_05_519Z-debug-0.log
remote:
remote: -----> Build failed
remote:        
remote:        We're sorry-this build is failing! You can troubleshoot common issues here:
remote:        https://devcenter.heroku.com/articles/troubleshooting-node-deploys
remote:
remote:        Some possible problems:
remote:
remote:        - Node version not specified in package.json
remote:          https://devcenter.heroku.com/articles/nodejs-support#specifying-a-node-js-version
remote:
remote:        Love,
remote:        Heroku
remote:
remote:  !     Push rejected, failed to compile Node.js app.
remote:
remote:  !     Push failed
remote:  !
remote:  ! ## Warning - The same version of this code has already been built: 377a2a25a1b56c226e11f9f9c45f371cc85adcea
remote:  !
remote:  ! We have detected that you have triggered a build from source code with version 377a2a25a1b56c226e11f9f9c45f371cc85adcea
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:  ! This article goes into details on the behavior:
remote:  !   https://devcenter.heroku.com/articles/duplicate-build-version
remote:
remote: Verifying deploy...
remote: 
remote: !       Push rejected to mern-tutorial-2022.
remote:
To https://git.heroku.com/mern-tutorial-2022.git
 ! [remote rejected] master -> master (pre-receive hook declined)
error: failed to push some refs to 'https://git.heroku.com/mern-tutorial-2022.git'

package.json

{
  "name": "mern-stack-2022",
  "version": "1.0.0",
  "description": "",
  "main": "server.js",
  "scripts": {
    "start": "node backend/server.js",
    "server": "nodemon backend/server.js",
    "client": "npm start --prefix frontend",
    "dev": "concurrently \"npm run server\" \"npm run client\"",
    "heroku-postbuild": "NPM_CONFIG_PRODUCTION=false npm install --prefix frontend && npm run build --prefix frontend"
  },
  "author": "Ope Afolabi",
  "license": "ISC",
  "dependencies": {
    "bcryptjs": "^2.4.3",
    "dotenv": "^16.0.0",
    "express": "^4.17.2",
    "express-async-handler": "^1.2.0",
    "jsonwebtoken": "^8.5.1",
    "mongoose": "^6.2.1"
  },
  "devDependencies": {
    "concurrently": "^7.0.0",
    "nodemon": "^2.0.15"
  }
}

server.js

const path = require('path')
const express = require('express')
const { errorHandler } = require('./middleware/errorMiddleware')
const dotenv = require('dotenv').config()
const port = process.env.PORT || 5000;
const connectDB = require('./config/db')

connectDB()

const app = express()

app.use(express.json())
app.use(express.json(express.urlencoded({ extended: false })))

app.use('/api/goals', require('./routes/goalRoutes'))
app.use('/api/users', require('./routes/userRoutes'))

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

    app.get('*', (req, res) => {
        res.sendFile(path.resolve(__dirname, '../', 'frontend', 'build', 'index.html'))
    })
} else {
    app.get('/', (req, res) => {
        res.send('Please set to production')
    })
}

app.use(errorHandler)
 
app.listen(port, () => console.log(`Server started on port ${port}`))

错误好像很清楚给你回复:

We have detected that you have triggered a build from source code with version 377a2a25a1b56c226e11f9f9c45f371cc85adcea remote: ! at least twice. One common cause of this behavior is attempting to deploy code from a different branch.

该字符串是您的捆绑包的散列,错误告诉您您已经部署了相同的捆绑包。 我的猜测是你之前从 develop 分支而不是 master 推送了同一个包。
我建议你做一个改变,创建一个新版本,这次从 master 分支推送。

这条消息:

remote:  ! If you are developing on a branch and deploying via git you must run:
remote:  !
remote:  !     git push heroku <branchname>:main

告诉您如何在开发过程中从不同的分支进行部署。

与其直接推送到 Heroku,不如在 Github 中创建存储库,然后转到 Heroku 仪表板,然后在部署选项卡中创建新应用程序,您可以将 GitHub 存储库连接到 Heroku您还可以启用自动部署。