Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I'm trying to deploy a node server to heroku and I'm getting this error: throw er; // Unhandled 'error' event

Tags:

node.js

heroku

I'm trying to deploy a node app to heroku and it looks like judging from other questions that heroku is dynamically assigning a port and it is somehow incompatible with something.

(I had to remove some details to post the question)

events.js:182
throw er; // Unhandled 'error' event
       ^

 Error: listen EACCES 0.0.0.0:80
     at Object._errnoException (util.js:1041:11)
     at _exceptionWithHostPort (util.js:1064:20)
     at Server.setupListenHandle [as _listen2] (net.js:1305:19)
     at listenInCluster (net.js:1370:12)
     at Server.listen (net.js:1466:7)
     at Function.listen (/app/node_modules/express/lib/application.js:618:24)
     at Object.<anonymous> (/app/index.js:9:21)
     at Module._compile (module.js:573:30)
     at Object.Module._extensions..js (module.js:584:10)
     at Module.load (module.js:507:32)
 npm ERR! code ELIFECYCLE
 npm ERR! errno 1
 npm ERR! [email protected] start: `node index.js`
 npm ERR! Failed at the [email protected] start script.
like image 231
F4Tornado Avatar asked Oct 30 '22 02:10

F4Tornado


2 Answers

Problem

I had the same issue. I ran into this problem when I tried to set the port manually:

app.listen(80, function () {
  console.log('Example blog app listening on port 80!')
});

This led me to the same EACCES error.

Solution

I was able to solve the problem by going back and looking through the Heroku docs where I found:

The command in a web process type must bind to the port number specified in the PORT environment variable. If it does not, the dyno will not start.

So, assuming you're using express, if you switch the port assignment to look something like this:

app.set('port', (process.env.PORT || 5000));


app.listen(app.get('port'), function() {
  console.log('Node app is running on port', app.get('port'));
});

Then you application should start properly.

Solution without Express.js

If you are not using Express, then you can set the port simply as:

var PORT = process.env.PORT || 5000;

Where process.env.PORT will be your production port, and 5000 will be the port you would use when testing your server locally.

like image 168
Zeralore Avatar answered Nov 15 '22 06:11

Zeralore


I have faced this kind of problem. The exact error is the port 80 is already enabled in another project or app. So before start your app, stop the other app mapped to 80 port. Now you can run your app without this error.

Thanks

like image 29
Vel Avatar answered Nov 15 '22 05:11

Vel