React项目更新路由后Heroku部署失败:提示index.html缺失
React项目部署Heroku提示index.html缺失(本地文件存在)
我是编程新手,用create-react-app搭建项目,更新React路由后部署到Heroku时遇到问题。按照Node.js的Heroku指南操作,但部署时Heroku提示找不到index.html,可我本地确认这个文件是存在的。以下是部署日志:
-----> Build
Detected both "build" and "heroku-postbuild" scripts
Running heroku-postbuild> node-js-getting-started@0.3.0 heroku-postbuild > npm run build > node-js-getting-started@0.3.0 build > react-scripts build Could not find a required file. Name: index.html Searched in: /tmp/build_4858e5c3/public-----> Build failed
We're sorry this build is failing! You can troubleshoot common issues here: https://devcenter.heroku.com/articles/troubleshooting-node-deploys Some possible problems: - node_modules checked into source control https://devcenter.heroku.com/articles/node-best-practices#only-git-the-important-bits Love, Heroku! Push rejected, failed to compile Node.js app.
! Push failed
解决建议
- 检查Git忽略规则:确认
.gitignore文件里没有误把public/index.html排除在外。如果规则里包含public/或者具体的index.html,会导致文件无法提交到Git仓库,Heroku自然找不到。 - 验证文件路径大小写:本地Windows/macOS环境不区分文件名大小写,但Heroku用的Linux环境严格区分。检查
public文件夹名是否为全小写,index.html文件名是否正确(不要写成Index.html或其他变体)。 - 确认Git追踪状态:在本地项目根目录运行
git ls-files public,查看输出里是否包含index.html。如果没有,执行git add public/index.html,提交后再重新推送部署。 - 核对项目目录结构:确保
public文件夹在项目根目录下,和src文件夹同级。create-react-app要求public必须处于根目录,否则react-scripts build会找不到指定的index.html文件。 - 清理缓存重新构建:本地先运行
npm run build,确认能正常生成build文件夹。如果本地构建正常,执行heroku builds:cache:purge清除Heroku的构建缓存,再重新推送代码。 - 检查package.json配置:确认
scripts里的build命令是react-scripts build,没有自定义错误的路径参数。
内容的提问来源于stack exchange,提问作者avatar
相关产品推荐
相关产品推荐

