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

后端 未结 30 1396
深忆病人
深忆病人 2020-12-02 03:47

I am learning angular 2 and for the first time I am using the angular CLI project to create a sandbox project.

I was able to run the command \"ng serve\" and it wor

30条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-02 04:14

    Open your cmd.exe as administrator,

    then Find the PID of port 4200

    netstat -ano | findstr :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.

    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
    

提交回复
热议问题