If you close your Terminal window in the wrong way or it crashed, there are instances where your local server process is still running which prevent you from using the same port. Before you restart your computer or start killing off processes in Activity Manager, try this out:
List open files lsof, filtering on internet addresses -i :<PORT>.
lsof - i :3000
From the list, look for the command that's running your server. For me, 99% of the time it'll be
node. Get the corresponding <PID> from the list and kill it.
By default, kill will gracefully try to end the process using the
-15 flag, but if the process doesn't die, you can use the
-9 flag to kill it immediately.
kill -9 420
I found a script on this "Members Only" 🙄 Medium post, which I think is the way to go for convenience, but I also feel this is simple enough to perform without a script, this way you aren't dependent on non-native scripts.
I think it could be a good exercise in scripting to make your own. His script isn't explained, so I'm wondering if the temporary text file is necessary. It also uses -9 by default.
This is a glorified re-posting of a Stack Overflow question. I've just searched this so many times, I wanted my own personal reference. 🙂