难以将我的 MERN 应用程序部署到 Heroku

时间:2021-06-24 11:16:07

标签: reactjs heroku react-redux mern heroku-cli

我无法在 Heroku 上部署我的应用程序,并且每次尝试将我的代码推送到 Heroku 时都会收到以下错误。

首先,我使用命令 heroku create 创建了我的 Heroku 应用程序。然后我将所有修改过的文件添加并提交到 git 暂存区,最后当我尝试使用 git 推送到 Heroku 时,它在构建后部分失败。

我用于推送代码的命令:git push heroku master

我遇到的错误:

remote: -----> Build
remote:        Running heroku-postbuild
remote:        
remote:        > dev-community-starter@1.0.0 heroku-postbuild /tmp/build_d493d086
remote:        > NPM_CONFIG_PRODUCTION=false npm install --prefix client && npm run build --prefix client
remote:        
remote:        
remote:        > core-js@2.6.12 postinstall /tmp/build_d493d086/client/node_modules/babel-runtime/node_modules/core-js
remote:        > node -e "try{require('./postinstall')}catch(e){}"
remote:        
remote:        
remote:        > core-js@3.8.1 postinstall /tmp/build_d493d086/client/node_modules/core-js
remote:        > node -e "try{require('./postinstall')}catch(e){}"
remote:        
remote:        
remote:        > core-js-pure@3.8.1 postinstall /tmp/build_d493d086/client/node_modules/core-js-pure
remote:        > node -e "try{require('./postinstall')}catch(e){}"
remote:        
remote:        
remote:        > ejs@2.7.4 postinstall /tmp/build_d493d086/client/node_modules/ejs
remote:        > node ./postinstall.js
remote:        
remote:        added 1937 packages from 730 contributors and audited 1942 packages in 48.194s
remote:        
remote:        123 packages are looking for funding
remote:          run `npm fund` for details
remote:        
remote:        found 5928 vulnerabilities (209 moderate, 5719 high)
remote:          run `npm audit fix` to fix them, or `npm audit` for details
remote:        
remote:        > client@0.1.0 build /tmp/build_d493d086/client
remote:        > react-scripts build
remote:        
remote:        Creating an optimized production build...
remote: Browserslist: caniuse-lite is outdated. Please run:
remote: npx browserslist@latest --update-db
remote: 
remote: Why you should do it regularly:
remote: https://github.com/browserslist/browserslist#browsers-data-updating
remote:        Failed to compile.
remote:        
remote:        ./src/utils/api.js
remote:        Cannot find module: 'axios'. Make sure this package is installed.
remote:        
remote:        You can install this package by running: yarn add axios.
remote:        
remote:        
remote: npm ERR! code ELIFECYCLE
remote: npm ERR! errno 1
remote: npm ERR! client@0.1.0 build: `react-scripts build`
remote: npm ERR! Exit status 1
remote: npm ERR! 
remote: npm ERR! Failed at the client@0.1.0 build 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.DNuM9/_logs/2021-06-24T11_06_31_565Z-debug.log
remote: npm ERR! code ELIFECYCLE
remote: npm ERR! errno 1
remote: npm ERR! dev-community-starter@1.0.0 heroku-postbuild: `NPM_CONFIG_PRODUCTION=false npm install --prefix client && npm run build --prefix client`
remote: npm ERR! Exit status 1
remote: npm ERR! 
remote: npm ERR! Failed at the dev-community-starter@1.0.0 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.DNuM9/_logs/2021-06-24T11_06_31_594Z-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:        - A module may be missing from 'dependencies' in package.json
remote:          https://devcenter.heroku.com/articles/troubleshooting-node-deploys#ensure-you-aren-t-relying-on-untracked-dependencies
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: b76c1f2afa5b943b747dc113ff0dc1004c044030
remote:  !
remote:  ! We have detected that you have triggered a build from source code with version b76c1f2afa5b943b747dc113ff0dc1004c044030
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 intense-reef-14591.
remote: 
To https://git.heroku.com/intense-reef-14591.git
 ! [remote rejected] master -> master (pre-receive hook declined)
error: failed to push some refs to 'https://git.heroku.com/intense-reef-14591.git'

谁能指导我如何解决这个错误?

2 个答案:

答案 0 :(得分:1)

我想你已经检查过以下问题了吧?

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:        - A module may be missing from 'dependencies' in package.json
remote:          https://devcenter.heroku.com/articles/troubleshooting-node-deploys#ensure-you-aren-t-relying-on-untracked-dependencies

您也可以尝试在推送中不包含 node_modules 文件夹。

还尝试通过 heroku web 和 github repo 推送它

答案 1 :(得分:1)

根据错误日志,axios 软件包未作为依赖项安装在您的 package.json 中。

可能您已经在设备上全局安装了 Axios,因此它可以在本地运行,但在 Heroku 部署期间,它无法找到该依赖项。

所以通过运行命令安装它:

npm install axios

然后再试一次。如果同样的问题仍然存在,请共享您的 package.json 文件。