"Port 4200 is already in use" when running the ng serve command

Solution 1:

This is what I used to kill the progress on port 4200

For linux users:

sudo kill $(sudo lsof -t -i:4200)

You could also try this:

sudo kill `sudo lsof -t -i:4200`

For windows users:

Port number 4200 is already in use. Open the cmd as administrator. Type below command in cmd:

netstat -a -n -o

And then, find port with port number 4200 by right click on terminal and click find, enter 4200 in "find what" and click "find next": Let say you found that port number 4200 is used by pid 18932. Type below command in cmd:

taskkill -f /pid 18932

For UNIX:

alias ngf='kill -9  $(lsof -t -i:4200);ng serve'

Now run ngf (instead of ng serve) in terminal from the project folder. This will kill all processes using the port 4200 and runs your Angular project.

Solution 2:

Open your cmd.exe as administrator,

then Find the PID of port 4200

netstat -ano | findstr :4200

Pid for port 4200

Here i have 3 PID :

  • Red one is from "ng-serve" (127.0.0.1:4200) that LISTENING
  • Green one is from "your browser"

kill only port 4200 (kill the red PID):

taskkill /PID 15940 /F

note : kill the green one will only lead your browser closed by force.

taskkill pid 15940

now you can do "ng-serve" to start your angular app at the same port 4200




Additional Stuff :

One liner : After looking a way to optimize this, Here is the One-liner command of this answer : (special thanks to : Josep Alsina for this tips)

for /f "tokens=5" %a in ('netstat -ano ^| find "4200" ^| find "LISTENING"') do taskkill /f /pid %a

Solution 3:

On Mac OS X you need the following command:

sudo lsof -t -i tcp:4200 | xargs kill -9

Remember you need to kill Angular's web server with Command+C.