部署Express.js+React.js项目到Heroku时遇模块缺失错误求助
Fixing "Cannot find module './school'" on Heroku Deployment
Hey there, let's get your app up and running on Heroku quickly—your error log tells us exactly what's going wrong here.
The Root Cause
Looking at your logs, the critical line that's breaking your deployment is:
Error: Cannot find module './school'
This almost always happens for one of two simple reasons:
- Case sensitivity mismatch: Local development (especially on Windows) uses a case-insensitive file system, so
require('./school')will findSchool.jsorSCHOOL.jsjust fine. But Heroku runs on Linux, which is strictly case-sensitive—so the filename has to match exactly what's in yourrequirestatement. - Missing file in Git: The
school.jsfile exists on your local machine, but you never added or committed it to Git, so it doesn't get pushed to Heroku during deployment.
Step-by-Step Fixes
1. Verify File Existence & Case Matching
- Navigate to your
controllersdirectory and check the exact filename of your school controller. Is itschool.js,School.js, or something else? - Open
/controllers/index.js(the file throwing the error) and look at the first require statement—it should look like:
Make sure the path here matches your actual filename exactly (including uppercase/lowercase letters). If your file isconst schoolController = require('./school');School.js, update the require torequire('./School'), or rename the file toschool.jsto match the existing require statement.
2. Ensure the File is Committed to Git
- Run
git statusin your project root. Look for any untracked files—ifschool.js(or your renamed file) is listed, it means it hasn't been added to Git yet. - Add and commit the file:
git add controllers/school.js # Replace with your actual file path git commit -m "Add missing school controller file"
3. Re-deploy to Heroku
- Push your updated code to Heroku:
git push heroku main # Use "master" instead of "main" if that's your default branch - Once deployment finishes, check the logs again with
heroku logs --tailto confirm the module error is gone. Then visit your app atxxxxx.herokuapp.com—it should no longer show the "Application error" page.
Extra Tips for Heroku Deployment
- Always double-check case sensitivity for all file paths when deploying to Linux-based platforms like Heroku—this is a super common gotcha!
- Make sure all backend dependencies are listed in your
package.json(Heroku installs only what's in this file). - For your PostgreSQL/Sequelize setup later, ensure your app is using Heroku's
DATABASE_URLenvironment variable instead of local credentials.
内容的提问来源于stack exchange,提问作者Idan
相关产品推荐
相关产品推荐

