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

执行git push heroku main时npm install流程无限循环的问题求助

问题描述

尝试从项目根目录将应用部署到Heroku时,构建过程中install命令陷入无限循环,重复执行cd server && npm install && cd ../client && npm install。

相关日志

remote:        > mygym-gym@1.0.0 install
remote:        > cd server && npm install && cd ../client && npm install
remote:
remote:        
remote:        added 301 packages, and audited 302 packages in 4s
remote:
remote:        16 packages are looking for funding
remote:          run `npm fund` for details
remote:
remote:        found 0 vulnerabilities
remote:        
remote:        > mygym-gym@1.0.0 install
remote:        > cd server && npm install && cd ../client && npm install
remote:
remote:        
remote:        up to date, audited 302 packages in 820ms
remote:
remote:        16 packages are looking for funding
remote:          run `npm fund` for details

项目配置

根目录package.json脚本配置

"scripts": {
    "start": "node server/server.js",
    "develop": "concurrently \"cd server && npm run watch\" \"cd client && npm start\"",
    "install": "cd server && npm install && cd ../client && npm install",
    "seed": "cd server && npm run seed",
    "build": "cd client && npm run build"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "concurrently": "^5.1.0"
  }

server目录package.json脚本配置

"scripts": {
    "start": "node server.js",
    "watch": "nodemon server.js",
    "seed": "node config/seeds.js"
  }

client目录package.json脚本配置

"scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  }

此前部署过类似应用,首次遇到该问题,求问循环原因及解决办法。


原因分析与解决方案

核心原因

Heroku的构建逻辑中,执行npm install安装根目录依赖时,会自动触发根目录package.json里名为install的自定义脚本。而你的这个脚本又会去执行server和client目录的npm install,当子目录的依赖安装完成后,Heroku会检测到项目文件有变动,再次触发根目录的npm install,进而又调用自定义的install脚本,形成无限循环。

解决办法

方案1:重命名根目录的install脚本

不要用install这个npm特殊钩子名称,改成比如install-all,再通过postinstall自动触发:

"scripts": {
    "start": "node server/server.js",
    "develop": "concurrently \"cd server && npm run watch\" \"cd client && npm start\"",
    "install-all": "cd server && npm install && cd ../client && npm install",
    "seed": "cd server && npm run seed",
    "build": "cd client && npm run build",
    "postinstall": "npm run install-all"
  }

这样Heroku执行默认npm install时不会触发你的自定义脚本,而postinstall会在根目录依赖安装完成后自动执行子目录的安装,完全避开循环。

方案2:指定Heroku构建命令

在根目录package.json里添加heroku-postbuild脚本,让Heroku直接执行自定义的安装+构建流程,跳过默认的循环触发逻辑:

"scripts": {
    // ... 其他原有脚本
    "install-all": "cd server && npm install && cd ../client && npm install",
    "heroku-postbuild": "npm run install-all && npm run build"
  }

Heroku会自动识别并执行heroku-postbuild作为构建环节的核心命令,不会再重复触发根目录的npm install。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.11 10:25:24