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

通过Heroku CLI部署MERN应用时遇react-scripts权限拒绝求助

解决Heroku部署MERN应用时的react-scripts: Permission denied错误

部署MERN应用到Heroku时遇到react-scripts: Permission denied错误,执行npm install react-scripts --save后问题仍存在,完整错误日志如下:

remote: -----> 构建
remote:        执行heroku-postbuild
remote:        
remote:        > backend@1.0.0 heroku-postbuild
remote:        > cd client && npm install --legacy-peer-deps && npm run build
remote:
remote:        
remote:        已移除9个包,3秒内完成1465个包的审计
remote:
remote:        217个包正在寻求资金支持
remote:          执行`npm fund`查看详情
remote:
remote:        6个高严重性漏洞
remote:
remote:        要解决所有问题(包括破坏性变更),执行:
remote:          npm audit fix --force
remote:
remote:        执行`npm audit`查看详情。
remote:        
remote:        > react-login@1.0.0 build
remote:        > react-scripts build
remote:
remote: /tmp/build-a0701c25.sh: 1: react-scripts: 权限被拒绝
remote:
remote: -----> 构建失败
remote:        
remote:        很抱歉构建失败!你可以在这里排查常见问题:
remote:        https://devcenter.heroku.com/articles/troubleshooting-node-deploys
remote:
remote:        可能的问题:
remote:
remote:        - node_modules已提交到版本控制
remote:          https://devcenter.heroku.com/articles/node-best-practices#only-git-the-important-bits
remote:
remote:        此致,
remote:        Heroku
remote:
remote:  !     推送被拒绝,Node.js应用编译失败。
remote: 
remote:  !     推送失败
remote: 验证部署...
remote: !       推送被拒绝到react-login-system。
remote:
To https://git.heroku.com/react-login-system.git
 ! [remote rejected]     master -> master (pre-receive hook declined)
error: 无法推送一些引用到'https://git.heroku.com/react-login-system.git'

以下是针对性的解决方案:

  • 移除Git仓库中的node_modules
    Heroku明确提示了这个潜在问题,本地需确保node_modules被加入.gitignore,然后执行以下命令清理并提交:

    git rm -r --cached node_modules
    git commit -m "从仓库移除node_modules"
    git push heroku master
    
  • 重新安装react-scripts并修复权限
    进入client目录,彻底卸载后重新安装react-scripts,确保权限正确:

    cd client
    npm uninstall react-scripts
    npm install react-scripts --save
    git add package.json package-lock.json
    git commit -m "重新安装react-scripts并修复权限"
    git push heroku master
    
  • 用npx执行react-scripts
    直接调用react-scripts可能存在路径或权限问题,修改client目录下package.json的build命令为:

    "build": "npx react-scripts build"
    

    提交修改后再次推送部署。

  • 指定Node.js版本
    在项目根目录和client目录的package.json中添加engines字段,指定兼容的Node.js版本(比如18.x):

    "engines": {
      "node": "18.x"
    }
    

    确保版本在Heroku支持的范围内,避免因版本不兼容导致的权限或依赖问题。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.19 23:31:00