4

I have the following error message with node.js:

events.js:72 
throw er; // Unhandled 'error' event 
^ 
Error: listen EADDRINUSE 
at errnoException (net.js:904:11) 
at Server._listen2 (net.js:1042:14) 
at Server.wrappedListen2 [as _listen2] (/Users/rakutza/Documents/ParcelPuppy/PP/node_modules/newrelic/lib/instrumentation/core/net.js:16:46) 
at listen (net.js:1064:10) 
at Server.listen (net.js:1138:5) 
at EventEmitter.app.listen (/Users/rakutza/Documents/ParcelPuppy/PP/node_modules/express/lib/application.js:559:24) 
at Object.<anonymous> (/Users/rakutza/Documents/ParcelPuppy/PP/app.js:109:18) 
at Module._compile (module.js:456:26) 
at Object.Module._extensions..js (module.js:474:10) 
at Module.load (module.js:356:32)

Apparently the port number which listen() tries to bind the server to is already in use. How can I try another port or close the program using this port?

The app is working in production mode but once I deploy it on the server AWS BeanStalk crashes.

var slice = Array.prototype.slice;

/**
 * EventEmitter
 */

function EventEmitter() {
  if (!this._events) this._events = {};
}

EventEmitter.prototype.addListener = function(type, listener) {
  if (!this._events[type]) {
    this._events[type] = listener;
  } else if (typeof this._events[type] === 'function') {
    this._events[type] = [this._events[type], listener];
  } else {
    this._events[type].push(listener);
  }
  this._emit('newListener', [type, listener]);
};

EventEmitter.prototype.on = EventEmitter.prototype.addListener;

EventEmitter.prototype.removeListener = function(type, listener) {
  var handler = this._events[type];
  if (!handler) return;

  if (typeof handler === 'function' || handler.length === 1) {
    delete this._events[type];
    this._emit('removeListener', [type, listener]);
    return;
  }

  for (var i = 0; i < handler.length; i++) {
    if (handler[i] === listener || handler[i].listener === listener) {
      handler.splice(i, 1);
      this._emit('removeListener', [type, listener]);
      return;
    }
  }
};

EventEmitter.prototype.off = EventEmitter.prototype.removeListener;

EventEmitter.prototype.removeAllListeners = function(type) {
  if (type) {
    delete this._events[type];
  } else {
    this._events = {};
  }
};

EventEmitter.prototype.once = function(type, listener) {
  function on() {
    this.removeListener(type, on);
    return listener.apply(this, arguments);
  }
  on.listener = listener;
  return this.on(type, on);
};

EventEmitter.prototype.listeners = function(type) {
  return typeof this._events[type] === 'function'
    ? [this._events[type]]
    : this._events[type] || [];
};
Brad
  • 159,648
  • 54
  • 349
  • 530
user3464679
  • 309
  • 1
  • 3
  • 13
  • might be of some use : [how-to-kill-process-running-on-particular-port-in-linux](http://stackoverflow.com/questions/11583562/how-to-kill-process-running-on-particular-port-in-linux) – Mithun Satheesh May 11 '15 at 03:48

1 Answers1

4

Nowhere in your code are you showing where you are listening on a network socket...

In any case, for Beanstalk, you should use the PORT environment variable, accessible via process.env.PORT. If you don't, you have no guarantee of using the port that Beanstalk wants you to listen in on.

Beanstalk instances have their own Nginx proxies in front of your Node.js application. Your app isn't accessed directly. That's likely why you are running into a collision.

Brad
  • 159,648
  • 54
  • 349
  • 530