Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EADDRINUSE Heroku Error When Starting Node App

Tags:

node.js

heroku

I'm getting an EADDRINUSE error when Heroku tries to start my app.

I've looked at other questions on the subject, where they got the same error and I understand in theory it's related to another process existing on the same port. However, I cannot understand which process it would be in my application.

I only have one app being started, and the only other process that exists is Sequelize. It also seems Heroku tries multiple ports and all cause the EADDRINUSE error

This one instance of the error:

2018-08-06T01:55:39.346648+00:00 app[web.1]: events.js:183
2018-08-06T01:55:39.346654+00:00 app[web.1]: throw er; // Unhandled 'error' event
2018-08-06T01:55:39.346655+00:00 app[web.1]: ^
2018-08-06T01:55:39.346657+00:00 app[web.1]:
2018-08-06T01:55:39.346658+00:00 app[web.1]: Error: listen EADDRINUSE :::47216
2018-08-06T01:55:39.346660+00:00 app[web.1]: at Object._errnoException (util.js:992:11)
2018-08-06T01:55:39.346662+00:00 app[web.1]: at _exceptionWithHostPort (util.js:1014:20)
2018-08-06T01:55:39.346663+00:00 app[web.1]: at Server.setupListenHandle [as _listen2] (net.js:1355:14)
2018-08-06T01:55:39.346665+00:00 app[web.1]: at listenInCluster (net.js:1396:12)
2018-08-06T01:55:39.346666+00:00 app[web.1]: at Server.listen (net.js:1480:7)
2018-08-06T01:55:39.346668+00:00 app[web.1]: at Function.listen (/app/node_modules/express/lib/application.js:618:24)
2018-08-06T01:55:39.346670+00:00 app[web.1]: at Object.<anonymous> (/app/dist/index.js:72:5)
2018-08-06T01:55:39.346671+00:00 app[web.1]: at Module._compile (module.js:652:30)
2018-08-06T01:55:39.346673+00:00 app[web.1]: at Object.Module._extensions..js (module.js:663:10)
2018-08-06T01:55:39.346674+00:00 app[web.1]: at Module.load (module.js:565:32)
2018-08-06T01:55:39.346676+00:00 app[web.1]: at tryModuleLoad (module.js:505:12)
2018-08-06T01:55:39.346677+00:00 app[web.1]: at Function.Module._load (module.js:497:3)
2018-08-06T01:55:39.346679+00:00 app[web.1]: at Function.Module.runMain (module.js:693:10)
2018-08-06T01:55:39.346680+00:00 app[web.1]: at startup (bootstrap_node.js:191:16)
2018-08-06T01:55:39.346682+00:00 app[web.1]: at bootstrap_node.js:612:3
2018-08-06T01:55:39.363899+00:00 app[web.1]: npm ERR! code ELIFECYCLE
2018-08-06T01:55:39.365183+00:00 app[web.1]: npm ERR! errno 1

This is my index.ts:

import * as express from 'express';
import * as passport from 'passport';
import { Strategy } from 'passport-local';

import * as Models from './db';

passport.use(new Strategy(
  async function(username, password, cb) {
     ...
}));
console.log('before serializeUser');
passport.serializeUser(function(user: Users, cb) {
  ..
});

passport.deserializeUser(async function(id: number, cb) {
  ...
});


const app = express();


app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');

app.use(require('morgan')('combined'));
app.use(require('cookie-parser')('keyboard cat'));
app.use(require('body-parser').urlencoded({ extended: true }));


app.use(require('express-session')({ secret: 'keyboard cat', resave: false, saveUninitialized: false }));

app.use(passport.initialize());
app.use(passport.session());

app.get('/profile',
  require('connect-ensure-login').ensureLoggedIn(),
  async (req, res) => {
    ...
});


app.post('/order', async (req, res) => {
   ...
});

// Added this because another issue suggested this would fix the issue, but it didn't
app.set('port', (process.env.PORT));


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

And this is my package.json:

{
  "name": "club-menu-saludable",
  "version": "1.0.0",
  "description": "",
  "scripts": {
    "build": "tsc && mv dist dist_ && mv dist_/src dist && rm -rf dist_",
    "start": "NODE_DEBUG='http,net' node ./dist/index.js",
    "test": "jest",
    "postinstall": "yarn build",
    "dev": "nodemon -x ./node_modules/.bin/ts-node -w ./src src/index.ts"
  },
  "license": "ISC",
  "dependencies": {
    ...
  },
  "devDependencies": {
    ...
  },
  "jest": {
    ...
  }
}

Has anyone encountered this error? I don't know what else to do.

like image 858
user3517317 Avatar asked Aug 06 '18 02:08

user3517317


People also ask

How do I fix error listen Eaddrinuse address already in use 5000?

EADDRINUSE means that the port number which listen() tries to bind the server to is already in use. So, in your case, there must be running a server on port 80 already. If you have another webserver running on this port you have to put node. js behind that server and proxy it through it.

How do I start heroku node app?

Run the npm install command in your local app directory to install the dependencies that you declared in your package. json file. Start your app locally using the heroku local command, which is installed as part of the Heroku CLI. Your app should now be running on http://localhost:5000/.

Does heroku run npm start?

By default, Heroku runs npm start while starting deployed Node. js applications, but if you would like to run some other script from your package. json instead you just need to follow one simple step. And that's it!

Does heroku use yarn or npm?

Heroku uses the lockfiles, either the package-lock. json or yarn. lock , to install the expected dependency tree, so be sure to check those files into git to ensure the same dependency versions across environments. If you are using npm, Heroku will use npm ci to set up the build environment.


1 Answers

I realized this was me being tired after working for hours without stopping.

I had an extra app.listen which I was not seeing, even when I trimmed the file to post here I didn't see it.

So for anyone out there having this issue, IT IS indeed 2 processes trying to start with one heroku app, even if you're not seeing it.

like image 107
user3517317 Avatar answered Sep 24 '22 07:09

user3517317