This post goes over how to fix the error Address already in use
(EADDRINUSE).
Problem
Did you encounter the error when starting a server?
> Error: Address already in use
> Error: listen EADDRINUSE
This happens because the port is already bound to a server.
There are 2 things you can do:
- Start your server on a different port, or
- Free the port by killing the process associated with it.
Warning: If you choose the 2nd option, make sure you’re not killing anything important.
Solution
Get the pid
Use lsof
to get the process id or pid associated with the port:
lsof -ti :$PORT
Replace
$PORT
with the port number.
Kill the pid
To kill the process associated with the port:
kill $(lsof -ti :$PORT)
Replace
$PORT
with the port number.
There may be a scenario where you’ll need to pass the SIGKILL
signal to stop and kill the process immediately:
kill -9 $(lsof -ti :$PORT)
Replace
$PORT
with the port number.
Use sudo
if you don’t have permissions:
sudo kill -9 $(lsof -ti :$PORT)
Replace
$PORT
with the port number.
Unix users can accomplish the same thing with fuser
:
fuser -k $PORT/tcp
Replace
$PORT
with the port number.