If a local development server says a port is already in use, use macOS’s built-in lsof command to find the process listening on that TCP port. In Terminal, run lsof with the port number and a listening-state filter, then use the reported PID to inspect the process before deciding whether to stop it.
Find the process listening on a TCP port
Open Terminal and replace 3000 with the port from the error message. Enter the command as shown; the number is plain text, so no shell escaping is needed.
lsof -nP -iTCP:3000 -sTCP:LISTEN
lsof lists files opened by processes, including Internet sockets. Here, -iTCP:3000 selects TCP connections for that port, and -sTCP:LISTEN narrows the results to sockets waiting for connections. -nP asks lsof to display numeric addresses and port numbers rather than resolving names, which makes the result easier to compare with the port you searched. These options are documented in Apple’s open-source macOS lsof manual. Apple’s macOS lsof manual
A result may look like this; the names and numbers are illustrative, not a report from a particular Mac:
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
node 8421 sam 21u IPv6 ... 0t0 TCP *:3000 (LISTEN)
Read COMMAND as the process name and PID as its process ID. The NAME field shows the local address and port. *:3000 indicates a wildcard address, while localhost:3000 or a numeric loopback address indicates a loopback binding. A wildcard listener can accept connections addressed to multiple local interfaces; it does not by itself establish that the service is reachable from the internet. The listening filter is useful because a process can also have ordinary client connections involving that same port.
Check what the PID belongs to
Use the PID from the output to request more detail from macOS’s process list. Replace 8421 with the PID you found:
ps -p 8421 -o pid=,ppid=,user=,command=
This displays the process ID, parent process ID, account name, and command line. The command line can help identify whether the listener belongs to a development server, a runtime, or another application. A short COMMAND value in lsof is not always enough to identify the project or the app that launched it, so compare the fuller ps output with the Terminal tab or application you expect to own the server.
If lsof returns no rows, check that the port number is right and that the application is still running. The server may have exited, may be using UDP rather than TCP, or may not yet have opened its listening socket. To inspect UDP sockets on that port, run:
lsof -nP -iUDP:3000
UDP does not use TCP’s LISTEN state, so do not add -sTCP:LISTEN to this query. If a result appears, use its COMMAND and PID as clues, then inspect that PID with ps as above.
If the command shows no result or permission error
A process running under another account may not be visible to your current user. If you get a permission-related message or suspect another user owns the socket, rerun the TCP query with administrator privileges:
sudo lsof -nP -iTCP:3000 -sTCP:LISTEN
Terminal will ask for your Mac account password. When you type it, characters usually do not appear; press Return when finished. Use administrator access to inspect the result, then identify the owning app before taking action. If the command still shows nothing, retry while the port conflict is occurring: process state can change between the error and the lookup.
Stop the right application
Finding a PID identifies a process; it does not tell you whether it is safe to terminate. If you recognize the owner, stop the server using the application or Terminal session that started it, then retry your original command. This gives the app a chance to shut down normally. If you cannot tell what launched the process, investigate the command line or parent PID first rather than terminating an unfamiliar system or background process. lsof reports current socket ownership; it does not explain why an app started or which project configuration selected that port.
For command details on your installed macOS version, Terminal can display its local manual with man lsof; press q to exit. Apple’s Terminal guide describes using man to look up command documentation. Get started with Terminal on Mac
Sources
macOS lsof manual page — Apple Open Source
Get started with Terminal on Mac — Apple Support