Heroku编译成功但访问出现应用错误,Express/Node应用求助
Hey Simon, let's figure out why your app is throwing an error even after a successful deploy. Here are the most common fixes to check:
1. Fix the Port Assignment (Likely the Culprit!)
Looking at your code, there's a critical issue with how you're setting the port:
const port = '8080' || process.env.PORT;
Since '8080' is a non-empty string (a "truthy" value in JavaScript), it will always take priority over process.env.PORT. Heroku dynamically assigns a port to your app via the PORT environment variable, so hardcoding it this way breaks the app's ability to listen on the correct port.
Replace that line with this:
const port = process.env.PORT || 8080;
This tells the app to use Heroku's assigned port first, and fall back to 8080 for local testing.
2. Ensure Your package.json Has the Right Scripts and Dependencies
Heroku relies on your package.json to know how to run your app:
- Make sure you have a
startscript defined:{ "scripts": { "start": "node app.js" } } - Double-check that
expressis listed in thedependenciessection (not justdevDependencies). Heroku won't install dev dependencies by default, so if express is only there, your app will fail to start.
3. Check Your .gitignore File
Make sure .gitignore includes node_modules/. If you push your local node_modules folder to Heroku, it can cause conflicts with the dependencies Heroku installs, leading to unexpected errors.
4. Dig Into Heroku Logs for Exact Error Details
If the above fixes don't work, run this command in your terminal to see real-time logs from Heroku:
heroku logs --tail
This will show you the exact error message (like missing modules, port conflicts, etc.) which can pinpoint the issue if the above steps don't resolve it.
Give these steps a try—bet the port fix gets you up and running!
内容的提问来源于stack exchange,提问作者Simon Kinn Bjørnøy

