How to Find and Kill a Background Node Process
I'm sure you've run into this issue before.
You try to npm run start
a project up but you get an error that looks something like the following:
project git:(master) ✗ npm run start
> gatsby develop
/Users/khalilstemmler/Documents/Personal/current/solid-book-wiki/projects/wiki/node_modules/yoga-layout-prebuilt/yoga-layout/build/Release/nbind.js:53
throw ex;
^
Error: listen EADDRINUSE: address already in use 127.0.0.1:8000
at Server.setupListenHandle [as _listen2] (node:net:1372:16)
at listenInCluster (node:net:1420:12)
at GetAddrInfoReqWrap.doListen (node:net:1559:7)
at GetAddrInfoReqWrap.onlookup [as oncomplete] (node:dns:73:8)
Emitted 'error' event on Server instance at:
at emitErrorNT (node:net:1399:8)
at processTicksAndRejections (node:internal/process/task_queues:83:21) {
code: 'EADDRINUSE',
errno: -48,
syscall: 'listen',
address: '127.0.0.1',
port: 8000
}
What's going on here?
You have another process running on port 8000
and whatever you're trying to spin up also needs to utilize that port.
You can tell this from the
EADDRINUSE
error constant and the fact that it says127.0.0.1:8000
is alreaady in use.
What to do?
Here's a quick an easy fix to kill the process running so you can start something else up on that port.
1. Find what's running on the port
First thing is first. Use the lsof
command to see what's running.
We'll use it like this: lsof -i:<port>
.
wiki git:(master) ✗ lsof -i:8000
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
node 49516 khalilstemmler 27u IPv4 0xb0855a2b3b9a7a2b 0t0 TCP localhost:irdmi (LISTEN)
Just as I thought. Some node process running.
2. Take note of the process number
Take a look at the PID
column. That marks the process number.
wiki git:(master) ✗ lsof -i:8000
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
node 49516 khalilstemmler 27u IPv4 0xb0855a2b3b9a7a2b 0t0 TCP localhost:irdmi (LISTEN)
Copy it. You'll need it for the next step.
3. Kill whatever's running on that port by using the process number
Now, let's kill the process.
You can do so with kill -9 <process-number>
.
For example:
kill -9 49516
4. Confirm the process has been killed
Good.
Lastly, let's make sure that worked.
We can run the lsof
command again to see if there's a process running.
➜ wiki git:(master) ✗ lsof -i:8000
➜ wiki git:(master) ✗
Nothing! Looks like we did it.
5. Re-run whatever you were trying to run
And finally, re-run whatever you were trying to run on that port.
Done-zo.
Now get back to it.
Summary
Here's a summary.
- Use
lsof -i:<port>
to find the offending process id - Take note of the process number in table
- Use
kill -9 <process-number>
to kill the process at the port - Confirm it worked with
lsof -i:<port>
again. You should not see the same process.
Stay in touch!
Join 20000+ value-creating Software Essentialists getting actionable advice on how to master what matters each week. 🖖
View more in Tooling