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

Heroku部署应用报错:启动脚本缺失致崩溃及GET请求失败求助

Heroku部署应用启动失败问题排查与解决

问题描述

在Heroku平台部署应用时持续报错,无法通过修改源码解决。推测启动报错原因是package.json的scripts中仅配置了"start:backend": "nodemon backend.js"和"start:frontend": "react-scripts start",缺少默认的start脚本。同时不清楚路径及GET请求出现了什么问题。

错误日志

2022-08-03T23:37:55.245406+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=calm-stream-00694.herokuapp.com request_id=f48450ec-3ead-40fb-8ccc-f25532f1fe0a fwd="99.225.37.41" dyno= connect= service= status=503 bytes= protocol=https
2022-08-03T23:37:55.445318+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=calm-stream-00694.herokuapp.com request_id=b90566e5-a089-45ad-95bb-a91da389f92c fwd="99.225.37.41" dyno= connect= service= status=503 bytes= protocol=https
2022-08-03T23:39:13.971134+00:00 heroku[web.1]: State changed from crashed to starting
2022-08-03T23:39:26.279431+00:00 heroku[web.1]: Starting process with command `npm run start`
2022-08-03T23:39:27.778014+00:00 heroku[web.1]: Process exited with status 1
2022-08-03T23:39:27.861645+00:00 heroku[web.1]: State changed from starting to crashed
2022-08-03T23:39:27.872168+00:00 heroku[web.1]: State changed from crashed to starting
2022-08-03T23:39:27.617450+00:00 app[web.1]: npm WARN config production Use `--omit=dev` instead.
2022-08-03T23:39:27.634032+00:00 app[web.1]: npm ERR! Missing script: "start"
2022-08-03T23:39:27.634167+00:00 app[web.1]: npm ERR!
2022-08-03T23:39:27.634286+00:00 app[web.1]: npm ERR! Did you mean one of these?
2022-08-03T23:39:27.634412+00:00 app[web.1]: npm ERR!     npm star # Mark your favorite packages
2022-08-03T23:39:27.634452+00:00 app[web.1]: npm ERR!     npm stars # View packages marked as favorites
2022-08-03T23:39:27.634492+00:00 app[web.1]: npm ERR!
2022-08-03T23:39:27.634598+00:00 app[web.1]: npm ERR! To see a list of scripts, run:
2022-08-03T23:39:27.634599+00:00 app[web.1]: npm ERR!   npm run
2022-08-03T23:39:27.636041+00:00 app[web.1]:
2022-08-03T23:39:27.636210+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2022-08-03T23:39:27.636264+00:00 app[web.1]: npm ERR!     /app/.npm/_logs/2022-08-03T23_39_27_592Z-debug-0.log
2022-08-03T23:39:40.250299+00:00 heroku[web.1]: Starting process with command `npm run start`
2022-08-03T23:39:41.591800+00:00 heroku[web.1]: Process exited with status 1
2022-08-03T23:39:41.660469+00:00 heroku[web.1]: State changed from starting to crashed

解决方案

1. 修复缺失的start脚本

Heroku默认执行npm run start启动应用,你的package.json里无此脚本,这是核心报错原因。根据项目架构分两种情况处理:

情况一:前后端分开部署(推荐)

  • 后端单独部署:修改package.json,添加"start": "node backend.js"(生产环境别用nodemon,它是开发工具,会占用额外资源)。
  • 前端单独部署:前端build后部署到Heroku静态资源服务,scripts里添加"start": "serve -s build",同时安装serve依赖(npm install --save serve)。

情况二:前后端同进程启动(适合小型项目)

用concurrently工具同时启动前后端:

  • 安装依赖:npm install --save concurrently
  • 修改package.json的scripts:
    "scripts": {
      "start:backend": "node backend.js",
      "start:frontend": "react-scripts start",
      "start": "concurrently \"npm run start:backend\" \"npm run start:frontend\"",
      "build": "react-scripts build"
    }
    
    更合理的生产环境做法:先build前端,再让后端托管静态文件。比如用Express的话,在backend.js里添加:
    const path = require('path');
    app.use(express.static(path.join(__dirname, 'build')));
    
    app.get('/*', (req, res) => {
      res.sendFile(path.join(__dirname, 'build', 'index.html'));
    });
    
    然后scripts里的start改为"start": "node backend.js",添加"heroku-postbuild": "npm run build",让Heroku部署时自动构建前端。

2. 路径与GET请求问题

日志里的H10错误是应用启动失败导致的,修复start脚本后,路径和GET请求问题大概率会自动解决。若仍有404问题:

  • 确保前端路由和后端接口路径不冲突。
  • 用React Router的话,配置后端返回index.html处理所有路由(如上面Express的代码)。
  • 检查后端接口是否正确处理GET请求,端口使用Heroku提供的process.env.PORT(别硬写端口号)。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.24 05:27:32