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

Heroku编译成功但访问出现应用错误,Express/Node应用求助

Troubleshooting Your Express App Error on Heroku

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 start script defined:
    {
      "scripts": {
        "start": "node app.js"
      }
    }
    
  • Double-check that express is listed in the dependencies section (not just devDependencies). 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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.27 03:49:07