Address Already in Use
Another socket is already bound to the specified address and port. The port may be in use by another process, or the socket may be in TCP TIME_WAIT state after a recent connection, preventing immediate reuse.
- 1Another process is already listening on the same port.
- 2The process crashed and restarted before the TCP TIME_WAIT period expired (default 60 seconds).
- 3Two instances of the same service were started simultaneously.
- 4The port was bound by a previous test run that is still in TIME_WAIT.
Starting a second server process on the same port.
$ node server.js & $ node server.js Error: listen EADDRINUSE: address already in use :::3000
expected output
Error: listen EADDRINUSE: address already in use :::3000
at Server.setupListenHandle [as _listen2] (node:net:1380:16)Fix 1
Set SO_REUSEADDR before binding
WHEN For server sockets that need to restart quickly without waiting for TIME_WAIT
// Node.js: use server.listen() with reuseAddr option // In C: int opt = 1; setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)); bind(sockfd, (struct sockaddr*)&addr, sizeof(addr));
Why this works
SO_REUSEADDR allows binding to a port that is in TIME_WAIT state, enabling fast server restarts.
Fix 2
Find and stop the process using the port
WHEN When the port is being used by a different process
# Find who is using port 3000 sudo ss -tlnp | grep :3000 sudo lsof -i :3000 # Kill the process kill $(lsof -t -i :3000)
Why this works
Stopping the process that owns the port releases the binding, allowing the new process to bind.
Linux Programmer Manual errno(3)
Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev