Heroku部署React应用遇H10错误:无法找到/app/console模块
Hey there, let's break down what's going on here and get your app up and running smoothly!
First: Fix the heroku run node console Error
That error happens because you're using the wrong command—console isn't a valid Node.js entry file. To access the Node.js interactive console on Heroku, use this instead:
heroku run node
That'll drop you right into the console without any module errors.
Next: Resolve the H10 "App Crashed" Error
The H10 code means your app is crashing right when it tries to start up. For React apps on Heroku, this usually ties back to startup scripts, build artifacts, or port configuration. Let's go through the common fixes:
1. Check Your package.json Startup Script
If you used create-react-app to build your project, Heroku needs a way to serve your static build files (not just run the dev server). Here's how to fix that:
- First, install
serve(a lightweight static file server) as a dependency:npm install --save serve - Then update your
scriptsinpackage.jsonto point to the build folder:"scripts": { "start": "serve -s build", "build": "react-scripts build", // Keep your other scripts like test, eject, etc. }
If you have a custom React setup with a backend server, make sure your start script points to your server entry file (like server.js) instead of a dev-only command.
2. Verify Your Build Artifacts Exist on Heroku
Even if your local build works, double-check that Heroku generated the build folder correctly. Run this command to list files on your Heroku dyno:
heroku run ls
Look for the build directory—if it's missing, check your Heroku build logs (even if you saw a "success" message, there might be hidden warnings) or re-run the build locally to ensure it works.
3. Use Heroku's Dynamic Port
Heroku assigns a random port to your app via the PORT environment variable—never hardcode a port like 3000.
- If you're using
serve, it automatically picks up thePORTvariable, so no extra work needed. - If you have a custom server, update your code to use the environment port:
const port = process.env.PORT || 3000; app.listen(port, () => { console.log(`Server running on port ${port}`); });
4. Get Detailed Crash Logs
The basic heroku logs --tail shows the H10 error, but not the root cause of the crash. To see exactly why your app is failing to start, run:
heroku logs --ps web.1
This will show the full startup logs for your web process, including any uncaught errors, missing dependencies, or configuration issues.
Wrap-Up
Start with fixing the console command, then work through the startup script, build folder, and port checks. The detailed web process logs will be your best friend for pinpointing any remaining issues.
内容的提问来源于stack exchange,提问作者Mitch JScriptorian

