Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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  
like image 812
Soccerlife Avatar asked Nov 08 '22 10:11

Soccerlife


1 Answers

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

like image 171
M Danil Rafiqi Avatar answered Nov 14 '22 21:11

M Danil Rafiqi