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

PM2在Heroku平台的NodeJS环境中无法正常工作的求助

Troubleshooting Heroku Deployment for Your Express/PM2 App

Hey there, let’s work through this Heroku deployment issue together— I’ve dealt with similar quirks when getting Node.js apps up on Heroku, so let’s break down the most likely fixes step by step.

First, let’s recap the core constraints Heroku imposes that might be tripping you up:

1. Fix the Port Binding (Critical!)

Heroku assigns a dynamic port to your app via the PORT environment variable— you can’t hardcode 3000 (or any fixed port) in your Express app. If you’re doing that, Heroku will refuse to route traffic to your app, which is a common silent failure.

Check your Express listen call in app.js (or your entry file) and make sure it looks like this:

app.listen(process.env.PORT || 3000, () => {
  console.log(`Server running on port ${process.env.PORT || 3000}`);
});

The || 3000 keeps your local setup working, while Heroku will use its own assigned port in production.

2. Restore the Correct Procfile

Switching to a worker dyno was a misstep— Heroku only routes HTTP traffic to web dynos. Workers are for background tasks (like queue processing) that don’t handle external requests. Revert your Procfile back to:

web: npm start

This tells Heroku to spin up a web dyno and run your npm start command to serve HTTP traffic.

3. Adjust Your PM2 Setup for Heroku

If your npm start script uses PM2, you need to use pm2-runtime instead of the regular pm2 start command. Heroku requires processes to run in the foreground (otherwise it’ll kill the process thinking it crashed), and pm2-runtime is designed specifically for container environments like Heroku.

Update your package.json scripts section to look like this:

{
  "scripts": {
    "start": "pm2-runtime start app.js", // For Heroku production
    "dev": "pm2 start app.js --watch" // Keep your local dev script as-is
  }
}

Also, make sure pm2 is listed in your dependencies (not devDependencies)— Heroku skips installing dev dependencies by default in production.

4. Use Heroku Logs to Find Exact Errors

The biggest mistake is guessing without checking logs. Run this command in your terminal to see real-time logs from your Heroku app:

heroku logs --tail

Look for keywords like:

  • EADDRINUSE or EACCES: Means you’re hardcoding a port instead of using process.env.PORT
  • Cannot find module: A dependency is missing (move it from devDependencies to dependencies)
  • Process exited with status 1: A general crash— read the lines above it for the root cause

5. Reset and Redeploy Cleanly

Sometimes dynos get into a weird state. Run these commands to reset everything:

  1. Restart your dynos: heroku restart
  2. Ensure you have exactly 1 web dyno running: heroku scale web=1
  3. Push your latest code to Heroku: git push heroku main (replace main with your branch name if needed)

Example Working Configs

For reference, here’s what your key files should look like:

package.json

{
  "name": "your-app-name",
  "version": "1.0.0",
  "dependencies": {
    "express": "^4.18.2",
    "pm2": "^5.3.0"
  },
  "scripts": {
    "start": "pm2-runtime start app.js",
    "dev": "pm2 start app.js --watch"
  }
}

Procfile

web: npm start

If you still hit issues, share the relevant snippets from heroku logs --tail— that’ll help pinpoint exactly what’s going wrong!

内容的提问来源于stack exchange,提问作者Soccerlife

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 09:49:17