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

后端 未结 30 1389
深忆病人
深忆病人 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:12
    ng serve --port 4201 --live-reload-port 4200
    

    and access using localhost:4201

    This should work as a temporary solution.

    or

    try listing port usage using
    lsof -i:4200
    and kill it manually using
    sudo kill -9 <Process PID using port 4200>

    0 讨论(0)
  • 2020-12-02 04:13
    netstat -anp | grep ":4200"
    

    This will tell you who's got the port.

    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2020-12-02 04:15

    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.

    0 讨论(0)
  • 2020-12-02 04:15

    I was facing the same issue every time I have to kill the port.

    I tried ./node_modules/.bin/ng serve --proxy-config proxy.conf.json --host 0.0.0.0 Instead of npm start and its works

    0 讨论(0)
  • 2020-12-02 04:16

    Kill process and close the terminal which you used for running the app on that port.

    0 讨论(0)
提交回复
热议问题