Soccerlife
Soccerlife

Reputation: 751

PM2 doesn't work on NodeJS (Heroku)

I'm using PM2 to let the NodeJS (Express) run continuously. On my local machine, the following works in my packages.json:

"scripts": {
    "start": "pm2 start ./bin/www"
}

After that, I use 'npm start' to run my app. This all works fine.

Now I want to deploy my app to Heroku. I added an Procfile with 'web: npm start'. After that I changed my packages.json a little bit to:

"scripts": {
    "preinstall": "npm install pm2 -g",
    "start": "pm2 start ./bin/www"
 }

However, I get this error:

2018-02-22T19:51:23.861641+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
2018-02-22T19:51:23.862201+00:00 heroku[web.1]: Stopping process with SIGKILL
2018-02-22T19:51:24.007776+00:00 heroku[web.1]: Process exited with status 137
2018-02-22T19:51:24.046849+00:00 heroku[web.1]: State changed from starting to crashed

I have looked up many ways to solve this problem such as changing my Procfile to 'worker: node app.js' and running the command: '$ heroku scale web=0 worker=1'.

But I can't find out where the problem lies. I think it has something to do with the 'pm2'-module, because when I don't use that, my app works fine. However it crashes then after a short period of time. Hope that somebody can help me out.

"pm2 show www" shows this:

│ status            │ online                                                       │
│ name              │ www                                                          │
│ restarts          │ 151                                                          │
│ uptime            │ 40s                                                          │
│ script path       │ /Users/user1/Documents/Weview/app/bin/www │
│ script args       │ N/A                                                          │
│ error log path    │ /Users/user1/.pm2/logs/www-error-1.log    │
│ out log path      │ /Users/user1/.pm2/logs/www-out-1.log      │
│ pid path          │ /Users/user1/.pm2/pids/www-1.pid          │
│ interpreter       │ node                                                         │
│ interpreter args  │ N/A                                                          │
│ script id         │ 1                                                            │
│ exec cwd          │ /Users/user1/Documents/Weview/app         │
│ exec mode         │ fork_mode                                                    │
│ node.js version   │ 8.9.1                                                        │
│ watch & reload    │ ✘                                                            │
│ unstable restarts │ 0                                                            │
│ created at        │ 2018-02-22T20:22:53.688Z                                     │
└───────────────────┴──────────────────────────────────────────────────────────────┘
 Code metrics value 
┌─────────────────┬────────┐
│ Loop delay      │ 2.86ms │
│ Active requests │ 0      │
│ Active handles  │ 4  

Upvotes: 6

Views: 5432

Answers (3)

M Danil Rafiqi
M Danil Rafiqi

Reputation: 584

the first, you must instal pm2. i read in these docs

cek your package.json

"scripts": { "preinstall": "npm install pm2 -g", "start": "pm2-runtime app.js -i max" },

Upvotes: 2

droid-zilla
droid-zilla

Reputation: 555

tl;dr You cannot use fork

I got it to work with one dyno by using "instances": 1 and "exec_mode": "cluster". It seems that when using fork with one dyno you cannot reuse the port as you only have one port and are running your app on a single thread, thus forking will fail due to EADDRINUSE. Check out this stackoverflow question for a thorough discussion of clustering and forking: Cluster and Fork mode difference in PM2

Upvotes: 1

codeofnode
codeofnode

Reputation: 18609

Use this

"scripts": {
    "start": "PORT=3000 pm2 start ./bin/www"
}

Heroku tries to bind node application with env variable PORT

Upvotes: 0

Related Questions