Learn how to troubleshoot and resolve the "port already in use" EADDRINUSE error, which commonly occurs when starting development servers like Node.js. This guide provides solutions for Windows, Mac, and Linux.
Identifying the Issue
The "EADDRINUSE" error occurs when an application tries to bind to a port that's already in use by another process. It typically appears during software development when starting local servers, such as Node.js on port 3000. To resolve this issue, you need to identify and terminate the process occupying the port.
Resolving the Error on Windows
To address the issue on Windows:
steps
Locate the process using the port:
bashnetstat -ano | findstr :3000This command lists the process ID (PID) associated with port 3000.
Terminate the process using its PID:
bashtaskkill /PID <PID> /FReplace
<PID>with the actual PID obtained in the previous step.Verify the port is free by restarting your server application.
Fixing the Error on Linux/Mac
On Linux or macOS systems, follow these steps:
steps
Identify which process is using port 3000:
bashlsof -i TCP:3000The output displays the PID and process name.
Terminate the process using its PID:
bashkill -9 <PID>Replace
<PID>with the PID obtained in the previous command.Confirm the port is no longer in use and restart your application.
Special Cases: Ghost Processes and Port Reservation on Windows
Takeaway
Resolving the "port already in use" EADDRINUSE error involves identifying and terminating the conflicting process. By using tools like netstat, taskkill, lsof, or kill, you can free the desired port and restart your application. Prevent similar issues by proactively checking active ports before assigning them in development.
FAQ
Why does the "EADDRINUSE" error occur frequently during development?
The error occurs when a development server, like Node.js, attempts to bind to a port that another process is already using. This commonly happens during frequent reboots or crashes of such servers.
How do I find which process is using a port without killing it?
On Windows, use netstat -ano | findstr :<PORT> and check the PID against running processes in Task Manager. On Linux/macOS, try lsof -i TCP:<PORT> to identify the process.
What if killing the process doesn't free the port?
This could indicate a reserved port (Windows) or a ghost process. Restart your system or use advanced utilities like netsh to remove reservations on Windows.
Official reference: Node.js API documentation.