"Address Already in Use": A Simple Guide to Freeing Up Ports

antonrosh

Anton Rosh

Posted on May 27, 2023

"Address Already in Use": A Simple Guide to Freeing Up Ports

As a developer, there's nothing quite as frustrating as being ready to launch your application, only to be greeted with an error like EADDRINUSE. This common error message indicates that the port you're trying to use is already occupied by another process. In this post, we'll go through the steps you can take to identify and kill the process that's tying up your port, whether you're on a Mac, Linux, or Windows system.

What does EADDRINUSE mean?

The EADDRINUSE error typically occurs when you're trying to start a server on a specific port, but that port is already being used by another process. This error is common across various languages and frameworks, including Node.js, Express.js, and in this case, Playwright with TypeScript.

Here's an example of the EADDRINUSE error:

Error: listen EADDRINUSE: address already in use 127.0.0.1:9323
    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) {
  code: 'EADDRINUSE',
  errno: -4091,
  syscall: 'listen',
  address: '127.0.0.1',
  port: 9323
}
Enter fullscreen mode Exit fullscreen mode

Identifying the Process

To free up the port, we first need to identify the process that's using it. Here's how you can do this on different operating systems:

Mac/Linux:

Open your terminal and type in:

sudo lsof -i :9323
Enter fullscreen mode Exit fullscreen mode

This command will give you a list of processes using port 9323, along with their PID (Process ID).

Windows:

Open the command prompt and type in:

netstat -ano | findstr :9323
Enter fullscreen mode Exit fullscreen mode

For example:

PS C:\Users\YourUser\YourProject> netstat -ano|findstr "PID :9323"           
  Proto  Local Address          Foreign Address        State           PID
  TCP    127.0.0.1:9323         0.0.0.0:0              LISTENING       27924
Enter fullscreen mode Exit fullscreen mode

This command will provide a list of processes using port 9323, along with their PID.

Killing the Process

Once you have the PID, you can kill the process:

Mac/Linux:

Suppose the PID you found was 27924, you'd type:

kill -9 27924
Enter fullscreen mode Exit fullscreen mode

Windows:

Again, if the PID was 27924, you'd type:

taskkill /PID 27924 /F
Enter fullscreen mode Exit fullscreen mode

Prevention is Better than Cure

While it's good to know how to solve the EADDRINUSE error, it's even better to prevent it from happening in the first place. Be mindful of the ports your applications are using and ensure they're appropriately closed when you're done.

Conclusion

The EADDRINUSE error can be a speed bump on your coding journey, but it's not an insurmountable one. With a little bit of terminal knowledge, you can quickly identify and eliminate the rogue process. So the next time this error pops up, you'll know exactly what

💖 💪 🙅 🚩
antonrosh
Anton Rosh

Posted on May 27, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related