如何解决持久节点错误 "EADDRINUSE"?

How to resolve persistent node error "EADDRINUSE"?

我正在尝试通过节点 运行 一个 http 服务器,但我一直收到特定端口 5000 的 EADDRINUSE 错误(尝试其他各种方法都可以正常工作)。我试过使用 sudo lsof -i tcp:5000sudo kill -9 [PID]。这是其中 shell 行之一:

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$ 

但我还是得到了

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

注意:我基于 npx express-generator 示例项目,其中包括错误:

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;
  }
}

使用 kill-port 5000; nodemon app.js 作为我的 npm run dev 脚本最终起作用的(主要是除了一点恐惧)。