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

Heroku部署执行cd frontend && npm run build时陷入无限循环

解决npm run build/Heroku部署时的无限循环问题

问题根源

执行npm run build或推送到Heroku时出现无限循环,是因为根目录的build脚本执行cd frontend && npm run build时,没有正确触发frontend子目录的build脚本,反而意外重复调用根目录的build命令,形成循环。

可能的具体原因

  • 目录名称不匹配:根目录develop/start脚本中用的是--prefix client,但你声明React应用在frontend目录,路径不一致会导致npm找不到子目录的package.json,转而使用根目录的配置重复执行build。
  • npm上下文切换失败:直接用cd切换目录后执行npm命令,可能因环境变量或配置问题,没有真正切换到子目录的package.json上下文。

解决方案

方案1:统一目录路径(如果实际目录是client)

如果你的React应用实际目录是client而非frontend,修改根目录package.json的脚本,将所有frontend替换为client:

"scripts": {
  "test": "echo \"Error: no test specified\" && exit 1",
  "build": "cd client && npm run build",
  "install-app": "cd client && npm install",
  "heroku-postbuild": "npm run install-app && npm run build",
  "app": "nodemon app.js",
  "develop": "concurrently --kill-others-on-fail \"npm run app\" \"npm run start --prefix client\"",
  "start": "concurrently --kill-others-on-fail \"npm run app\" \"npm run start --prefix client\""
}

方案2:使用--prefix参数指定子目录(更可靠)

避免cd命令的上下文问题,直接用npm的--prefix参数明确指定子目录,确保调用子目录的脚本:
修改根目录package.json的相关脚本:

"scripts": {
  "test": "echo \"Error: no test specified\" && exit 1",
  "build": "npm run build --prefix frontend",
  "install-app": "npm install --prefix frontend",
  "heroku-postbuild": "npm run install-app && npm run build",
  "app": "nodemon app.js",
  "develop": "concurrently --kill-others-on-fail \"npm run app\" \"npm run start --prefix frontend\"",
  "start": "concurrently --kill-others-on-fail \"npm run app\" \"npm run start --prefix frontend\""
}

方案3:确认子目录package.json的正确性

检查frontend目录下的package.json是否存在,且build脚本配置正确:

"scripts": {
  "build": "react-scripts build"
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.22 13:09:25