MERN 应用程序在部署到 Heroku 时无法运行“heroku-postbuild”脚本

Posted

技术标签:

【中文标题】MERN 应用程序在部署到 Heroku 时无法运行“heroku-postbuild”脚本【英文标题】:MERN app fails to run "heroku-postbuild" script while being deployed to Heroku 【发布时间】:2020-10-22 00:59:44 【问题描述】:

在我尝试将应用程序部署到 Heroku 之前,一切都运行良好。登录后,这些是我运行以部署应用程序(和响应)的终端命令:

heroku login
heroku: Press any key to open up the browser to login or q to exit: 
Logging in... done
Logged in as #hidden

heroku create
Creating app... done, ⬢ lower-donair-61893
https://lower-donair-61893.herokuapp.com/ | https://git.heroku.com/lower-donair-61893.git

git init
Initialized empty Git repository in #hidden

heroku git:remote -a lower-donair-61893

git add .
warning: adding embedded git repository: client
hint: You've added another git repository inside your current repository.
hint: Clones of the outer repository will not contain the contents of
hint: the embedded repository and will not know how to obtain it.
hint: If you meant to add a submodule, use:
hint: 
hint: 
hint: If you added this path by mistake, you can remove it from the
hint: index with:
hint: 
hint:   git rm --cached client
hint: 
hint: See "git help submodule" for more information.
warning: LF will be replaced by CRLF in package-lock.json.
The file will have its original line endings in your working directory

git commit -am "make it better"
[master (root-commit) cdaf621] make it better
 13 files changed, 2518 insertions(+)
 create mode 100644 .gitignore
 create mode 160000 client
 create mode 100644 config/default.json
 create mode 100644 models/Comment.js
 create mode 100644 models/Post.js
 create mode 100644 models/User.js
 create mode 100644 package-lock.json
 create mode 100644 package.json
 create mode 100644 routes/api/auth.js
 create mode 100644 routes/api/posts.js
 create mode 100644 routes/api/users.js
 create mode 100644 server.js

当我运行最后一个命令时,“乐趣”开始了:

git push heroku master
Enumerating objects: 19, done.
Counting objects: 100% (19/19), done.
Delta compression using up to 8 threads
Compressing objects: 100% (15/15), done.
Writing objects: 100% (19/19), 28.99 KiB | 1.61 MiB/s, done.
Total 19 (delta 3), reused 0 (delta 0), pack-reused 0
remote: Compressing source files... done.
remote: Building source:
remote: 
remote: -----> Node.js app detected
remote:        
remote: -----> Creating runtime environment
remote:
remote:        NPM_CONFIG_LOGLEVEL=error
remote:        NODE_ENV=production
remote:        NODE_MODULES_CACHE=true
remote:        NODE_VERBOSE=false
remote:
remote: -----> Installing binaries
remote:        engines.node (package.json):  unspecified
remote:        engines.npm (package.json):   unspecified (use default)
remote:
remote:        Resolving node version 12.x...
remote:        Downloading and installing node 12.18.2...
remote:        Using default npm version: 6.14.5
remote:        
remote: -----> Installing dependencies
remote:        Installing node modules (package.json + package-lock)
remote:        
remote:        > nodemon@2.0.4 postinstall /tmp/build_01ff90de74651f20444f369d014096d8/node_modules/nodemon
remote:        > node bin/postinstall || exit 0
remote:
remote:        Love nodemon? You can now support the project via the open collective:
remote:         > https://opencollective.com/nodemon/donate
remote:
remote:        added 267 packages from 151 contributors and audited 268 packages in 9.402s
remote:        
remote:        12 packages are looking for funding
remote:          run `npm fund` for details
remote:
remote:        found 0 vulnerabilities
remote:
remote:        
remote: -----> Build
remote:        Running heroku-postbuild
remote:        
remote:        > heroku-postbuild /tmp/build_01ff90de74651f20444f369d014096d8
remote:        > NPM_CONFIG_PRODUCTION=false npm install --prefix client && npm run build --prefix client
remote:
remote:        up to date in 0.219s
remote:        found 0 vulnerabilities
remote:
remote: npm ERR! code ENOENT
remote: npm ERR! syscall open
remote: npm ERR! path /tmp/build_01ff90de74651f20444f369d014096d8/client/package.json
remote: npm ERR! errno -2
remote: npm ERR! enoent ENOENT: no such file or directory, open '/tmp/build_01ff90de74651f20444f369d014096d8/client/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.0iNHH/_logs/2020-07-01T13_46_53_504Z-debug.log
remote: npm ERR! code ELIFECYCLE
remote: npm ERR! errno 254
remote: npm ERR! heroku-postbuild: `NPM_CONFIG_PRODUCTION=false npm install --prefix client && npm run build --prefix client`
remote: npm ERR! Exit status 254
remote: npm ERR!
remote: npm ERR! Failed at the heroku-postbuild script.
remote: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
remote:
remote: npm ERR! A complete log of this run can be found in:
remote: npm ERR!     /tmp/npmcache.0iNHH/_logs/2020-07-01T13_46_53_520Z-debug.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: Verifying deploy...
remote:
remote: !       Push rejected to lower-donair-61893.
remote:
To https://git.heroku.com/lower-donair-61893.git
 ! [remote rejected] master -> master (pre-receive hook declined)
error: failed to push some refs to 'https://git.heroku.com/lower-donair-61893.git'

所以这是一个 ENOENT,但我搜索了其他部署到 Heroku 的 MERN 应用程序并找到了这个视频: https://www.youtube.com/watch?v=71wSzpLyW9k&list=PLillGF-RfqbbiTGgA77tGO426V3hRF9iE&index=8 他在其中使用了与我相同的代码行。此外,我的应用程序是他系列的“继续”,因此文件结构、脚本等完全相同(这意味着如果您导航到视频的 desc,您可以看到指向 github 存储库的链接,该链接要少得多, 但是 package.json 等关键文件被克隆了)。

这是我正在尝试运行的构建后脚本:

"heroku-postbuild": "NPM_CONFIG_PRODUCTION=false npm install --prefix client && npm run build --prefix client"

这是我的文件结构的截图: file structure

过去 4 小时我一直在寻找答案,但一无所获。错误消息指出了这条路径,但我真的找不到它来查看完整的日志。

/tmp/npmcache.0iNHH/_logs/2020-07-01T13_46_53_504Z-debug.log 

如果您需要查看更多信息,请告诉我,我会很乐意提供您需要的任何东西 - 现在我将复制 package.json 文件(服务器和客户端)中的所有代码:

服务器一:


    "name": "#hidden",
    "version": "1.0.0",
    "description": "#hidden",
    "main": "server.js",
    "scripts": 
        "client-install": "npm install --prefix client",
        "start": "node server",
        "server": "nodemon server",
        "client": "npm start --prefix client",
        "dev": "concurrently \"npm run server\" \"npm run client\"",
        "heroku-postbuild": "NPM_CONFIG_PRODUCTION=false npm install --prefix client && npm run build --prefix client"
    ,
    "keywords": [
       #hidden
    ],
    "author": "#hidden",
    "license": "ISC",
    "dependencies": 
        "bcryptjs": "^2.4.3",
        "concurrently": "^5.2.0",
        "config": "^3.3.1",
        "express": "^4.17.1",
        "jsonwebtoken": "^8.5.1",
        "mongoose": "^5.9.19"
    ,
    "devDependencies": 
        "nodemon": "^2.0.4"
    


客户一:


    "name": "client",
    "version": "0.1.0",
    "private": true,
    "dependencies": 
        "@testing-library/jest-dom": "^4.2.4",
        "@testing-library/react": "^9.3.2",
        "@testing-library/user-event": "^7.1.2",
        "axios": "^0.19.2",
        "bootstrap": "^4.5.0",
        "moment": "^2.27.0",
        "react": "^16.13.1",
        "react-dom": "^16.13.1",
        "react-moment": "^0.9.7",
        "react-redux": "^7.2.0",
        "react-router-dom": "^5.2.0",
        "react-router-redux": "^4.0.8",
        "react-scripts": "3.4.1",
        "react-transition-group": "^4.4.1",
        "reactstrap": "^8.4.1",
        "redux": "^4.0.5",
        "redux-thunk": "^2.3.0"
    ,
    "scripts": 
        "start": "react-scripts start",
        "build": "react-scripts build",
        "test": "react-scripts test",
        "eject": "react-scripts eject"
    ,
    "eslintConfig": 
        "extends": "react-app"
    ,
    "browserslist": 
        "production": [
            ">0.2%",
            "not dead",
            "not op_mini all"
        ],
        "development": [
            "last 1 chrome version",
            "last 1 firefox version",
            "last 1 safari version"
        ]
    ,
    "proxy": "http://localhost:5000"

【问题讨论】:

【参考方案1】:

我设法解决了这个问题,并通过删除客户端文件夹中的所有默认 gitignore 异常成功部署了应用程序。我不知道为什么,但其中包含“/build”。我最终的客户端 gitignore 文件只包括:

/node_modules

【讨论】:

【参考方案2】:

如果是heroku postbuild问题,那么首先运行

npm run build

在客户端文件夹(前端应用程序)内的本地系统中,如果您的反应应用程序中缺少某些依赖项,它将引发错误,

如果构建命令成功,则删除 node_modules 和 package-lock.json 文件然后运行

npm install

【讨论】:

【参考方案3】:

检查 package.json 中的命令是否正确。对我来说,我有以下设置命令错误。 “heroku-postbuild”:“npm install && npm run build” 尝试阅读所有错误,因为它们通常非常清楚并准确指出哪个命令在哪一行失败。

【讨论】:

以上是关于MERN 应用程序在部署到 Heroku 时无法运行“heroku-postbuild”脚本的主要内容,如果未能解决你的问题,请参考以下文章

如何使用后端文件夹内的前端将 MERN 应用程序部署到 Heroku

为啥我在将 MERN 部署到 Heroku 期间错误地收到文件丢失错误?

在部署到 Heroku 之前,我应该对 MERN 应用程序的 package.json 文件的脚本进行哪些更改?

Craco 在 Heroku 中部署时无法构建反应应用程序

部署到 heroku - 白屏 - 没有明显错误 - MERN 堆栈在本地工作得很好,包括 API 请求 - 已经尝试了 27 次部署

在 heroku 上部署 MERN 会导致意外错误