Search code examples
javascriptnode.jsexpressserverlocalhost

How to resolve persistent node error "EADDRINUSE"?


I'm trying to run an http server through node but I keep getting the EADDRINUSE error with specifically port 5000 (trying various others works fine). I have tried using sudo lsof -i tcp:5000 and sudo kill -9 [PID]. Here is one of those shell lines:

Borealis:BackEnd grepgrok$ sudo lsof -i tcp:5000
COMMAND     PID  USER       FD   TYPE             DEVICE SIZE/OFF NODE NAME
ControlCe 34117  grepgrok   26u  IPv4 0x64883005c755e215      0t0  TCP *:commplex-main (LISTEN)
ControlCe 34117  grepgrok   27u  IPv6 0x64883005c59ef42d      0t0  TCP *:commplex-main (LISTEN)
Borealis:BackEnd grepgrok$ sudo kill -9 34117
Borealis:BackEnd grepgrok$ 

Yet I still get

Borealis:BackEnd grepgrok$ node ./bin/www
Port 5000 is already in use

Note: I am basing this off the npx express-generator sample project which includes this for errors:

function onError(error) {
  if (error.syscall !== 'listen') {
    throw error;
  }

  var bind = typeof port === 'string'
    ? 'Pipe ' + port
    : 'Port ' + port;

  // handle specific listen errors with friendly messages
  switch (error.code) {
    case 'EACCES':
      console.error(bind + ' requires elevated privileges');
      process.exit(1);
      break;
    case 'EADDRINUSE':
      console.error(bind + ' is already in use');
      process.exit(1);
      break;
    default:
      throw error;
  }
}

Solution

  • What ended up working (mostly except for a bit of funk) is using kill-port 5000; nodemon app.js as my npm run dev script.