PM2 doesn't work on NodeJS (Heroku)
Asked Answered
Q

3

6

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  
Quinsy answered 22/2, 2018 at 20:8 Comment(0)
H
2

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" },

Hildebrandt answered 17/8, 2018 at 3:46 Comment(0)
V
1

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

Vespid answered 6/3, 2018 at 6:59 Comment(1)
I solved the problem by completely not using PM2. There was a bug in my code with node-mysql which closed the server connection after 60 seconds. However, thank you for your help.Quinsy
T
0

Use this

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

Heroku tries to bind node application with env variable PORT

Teodoor answered 22/2, 2018 at 20:16 Comment(5)
I tried it, see my error in the updated version of my post. The error is quite vague and I really don't know where it goes wrong. Thank you for your help.Quinsy
Seems like bin/www is not a healthy application. Try pm2 show www and see what is the outcomeTeodoor
The 'script path' points a local path (/Users/....). Is this a correct? In the post you see the update.Quinsy
Please remove ./ and simply use bin/www, assuming wherever package.json resides you also have bin directory in parallelTeodoor
If not solved, post the output of /Users/user1/.pm2/logs/www-error-1.logTeodoor

© 2022 - 2024 — McMap. All rights reserved.