10

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.

user3517317
  • 319
  • 3
  • 12
  • Is this happening on heroku, locally? On heroku you need to pick up the port from an environment variable. – Robert Moskal Aug 06 '18 at 02:27
  • looks like he already picked up the port from `process.env.PORT`? – NoobTW Aug 06 '18 at 02:31
  • 1
    This is happening on Heroku, not locally. I'm already getting the PORT from the env variable: `app.set('port', (process.env.PORT));`. Also, I consoled log `process.env.PORT` before the error and it matches with the port that heroku is trying to use. – user3517317 Aug 06 '18 at 02:46
  • Maybe, is it possible that you have several servers listen on `process.env.PORT`? – NoobTW Aug 06 '18 at 02:51
  • The app is really simple, that's the only server listen I have. The folder structure is straightforward. There is only one file related to server so far, which is in `src/index.ts`. The other folders are for `db` and `config`. Also `process.env.PORT` is only used once, in the `app.set` of `index.ts` (posted in the original issue). – user3517317 Aug 06 '18 at 02:57

1 Answers1

10

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.

user3517317
  • 319
  • 3
  • 12