How to Unlock a Locked Port in Windows 10
Now and then when developing an Angular SPA with Visual Studio Code, I lock up the default port, 4200. When this happens, you are told you can just reconfigure the app to use a different port, but that’s no solution. It’s better to kill the process holding onto the port, which of course is just a VS Code debugging process that hung for some reason.
By the way, Googling the VS Code issue, I found it’s a known bug and may be a symptom of using VS Code’s integrated terminal to run ng serve. A possible fix is to turn off something called Conpty in the VS Code settings.
But say you have some other reason to free up a locked port? To do it, you first have to find the PID of the process using the port, then you need to kill the process using the PID. To see the process id (PID) of the process using a particular port (4200 in the example here) run this in a command window with admin privileges.
netstat -a -n -o | find “:4200” | find /I “listening”
You can read more about netstat here. In a nutshell,
Displays all active TCP connections and the TCP and UDP ports on which the computer is listening.
Displays active TCP connections, however, addresses and port numbers are expressed numerically and no attempt is made to determine names.
Displays active TCP connections and includes the process ID (PID) for each connection. You can find the application based on the PID on the Processes tab in Windows Task Manager. This parameter can be combined with -a, -n, and -p.
find /I just finds lines with the given text and outputs only them. See more about find here.
7400 on the right side of the listing is our PID (process id).
To kill the process
taskkill -f /pid 7400