How can I solve EADDRINUSE in nodejs?

Viewed 1197

I state that I'm using a linux Os in particular I'm using Garuda linux with linux-zen kernel. I have found this problem in my arch OS and in a debian Os (Ubuntu 21.10) but not in windows and in macOS. I created a javascript server in this way:

const host = 'localhost';
const port = 8000;
const http=require('http')
const fs = require('fs')
const express = require("express");
const cors = require("cors");
const app = express();



const requestListener = function (req, res) {
  res.writeHead(200);
  res.end()
}

const server = http.createServer(requestListener);

const corsOptions = {
  origin: "http://localhost:4200",
  optionsSuccessStatus: 200, // For legacy browser support
  methods: "GET, POST"
};
app.use(cors(corsOptions));


app.listen(port, function() {
  console.log("Listening on " + port);
});
server.listen(port, host, () => {
  console.log(`Server is running on http://${host}:${port}`);
});

When I try to run this server with node httpController.js my terminal gives me this error:

Listening on 8000
node:events:368
      throw er; // Unhandled 'error' event
      ^

Error: listen EADDRINUSE: address already in use ::1:8000
    at Server.setupListenHandle [as _listen2] (node:net:1330:16)
    at listenInCluster (node:net:1378:12)
    at GetAddrInfoReqWrap.doListen [as callback] (node:net:1516:7)
    at GetAddrInfoReqWrap.onlookup [as oncomplete] (node:dns:74:8)
Emitted 'error' event on Server instance at:
    at emitErrorNT (node:net:1357:8)
    at processTicksAndRejections (node:internal/process/task_queues:83:21) {
  code: 'EADDRINUSE',
  errno: -98,
  syscall: 'listen',
  address: '::1',
  port: 8000
}

Node.js v17.3.0

I thought that there was a server running in my port 8000 so I put in the terminal this command lsof -i tcp:8000 but it doesn't give me any response probably because ther isn't any server running in my port. To be more secure I change the port of my server but the response is the same (EADDRINUSE). What can I do?

P.S. This is the output of netstat -tulpn:

Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 127.0.0.1:46239         0.0.0.0:*               LISTEN      8612/app --node-int 
udp        0      0 0.0.0.0:59437           0.0.0.0:*                           9540/firefox        
udp        0      0 0.0.0.0:35238           0.0.0.0:*                           9540/firefox        
udp        0      0 0.0.0.0:52008           0.0.0.0:*                           9540/firefox        
udp        0      0 0.0.0.0:56124           0.0.0.0:*                           9540/firefox        
udp        0      0 0.0.0.0:43991           0.0.0.0:*                           -                   
udp        0      0 0.0.0.0:56391           0.0.0.0:*                           9540/firefox        
udp        0      0 0.0.0.0:56902           0.0.0.0:*                           9540/firefox        
udp        0      0 0.0.0.0:53119           0.0.0.0:*                           9540/firefox        
udp        0      0 0.0.0.0:41038           0.0.0.0:*                           9540/firefox        
udp        0      0 0.0.0.0:58315           0.0.0.0:*                           9540/firefox        
udp        0      0 0.0.0.0:5353            0.0.0.0:*                           -                   
udp        0      0 0.0.0.0:54602           0.0.0.0:*                           9540/firefox        
udp6       0      0 :::42912                :::*                                -                   
udp6       0      0 fe80::a715:e177:7d3:546 :::*                                -                   
udp6       0      0 :::5353                 :::*                                -          

This is the output of strace -e trace=network node httpController.js:

socket(AF_INET6, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, IPPROTO_IP) = 20
setsockopt(20, SOL_SOCKET, SO_REUSEADDR, [1], 4) = 0
setsockopt(20, SOL_IPV6, IPV6_V6ONLY, [0], 4) = 0
bind(20, {sa_family=AF_INET6, sin6_port=htons(8000), sin6_flowinfo=htonl(0), inet_pton(AF_INET6, "::", &sin6_addr), sin6_scope_id=0}, 28) = 0
listen(20, 511)                         = 0
Listening on 8000
socket(AF_INET6, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, IPPROTO_IP) = 21
setsockopt(21, SOL_SOCKET, SO_REUSEADDR, [1], 4) = 0
setsockopt(21, SOL_IPV6, IPV6_V6ONLY, [0], 4) = 0
bind(21, {sa_family=AF_INET6, sin6_port=htons(8000), sin6_flowinfo=htonl(0), inet_pton(AF_INET6, "::1", &sin6_addr), sin6_scope_id=0}, 28) = -1 EADDRINUSE (Indirizzo già in uso)
node:events:368
      throw er; // Unhandled 'error' event
      ^

Error: listen EADDRINUSE: address already in use ::1:8000
    at Server.setupListenHandle [as _listen2] (node:net:1330:16)
    at listenInCluster (node:net:1378:12)
    at GetAddrInfoReqWrap.doListen [as callback] (node:net:1516:7)
    at GetAddrInfoReqWrap.onlookup [as oncomplete] (node:dns:74:8)
Emitted 'error' event on Server instance at:
    at emitErrorNT (node:net:1357:8)
    at processTicksAndRejections (node:internal/process/task_queues:83:21) {
  code: 'EADDRINUSE',
  errno: -98,
  syscall: 'listen',
  address: '::1',
  port: 8000
}

Node.js v17.3.0
+++ exited with 1 +++
1 Answers

Use command netstat -tulpn to see in 8000 port which service is running. If there is any service running try to kill the service with sudo kill -9 process_id to kill the process. After killing the process try to run your server on port 8000. Hope this will solve your issue.

Related