297

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 works great. I wanted to stop it from running so I ran "Control Z".

When I tried to run the "ng-serve" command again it gives me "Port 4200 is already in use."

I ran "PS" to get a list of the PID and killed the PID for the angular-cli and ran "ng-serve" again still it gives the same port in use error.

4
  • 1
    A lifelong Windows developer I've spent the last few months on a Mac and trained myself to use CMD + Z. I started running into this problem back on Windows as I was defaulting to CTRL + Z instead of CTRL+ C Commented Sep 7, 2019 at 1:04
  • 2
    If you are working on visual studio code, the close it and restart it.
    – Kurkula
    Commented Jun 23, 2020 at 20:56
  • You can kill the process running in this port by checking the necessary commands for the OS that you're using or you can run the project in another port :) Commented Jul 13, 2021 at 23:44
  • 20
    npx kill-port 4200 Commented Sep 23, 2021 at 14:00

52 Answers 52

507

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.

14
  • 3
    While I realize that this is the correct solution, does this not seem to be a horrible solution? What if I want to run ng serve on a different directory? Doesn't make sense to kill the server this way and then restart it somewhere else or am I missing something? Commented May 2, 2017 at 4:49
  • 6
    You can specify a different port with: --port <new_port>
    – hestellezg
    Commented Jun 10, 2017 at 5:07
  • 4
    This kills the default browser. And not helpful.
    – shijin
    Commented Jun 20, 2017 at 13:08
  • 7
    this did murder my browser window. I figured it out eventually though... I had done a ^Z on an existing angular instance that was using the default port, silly me! Commented Jul 25, 2017 at 1:56
  • 2
    To quickly check which pid is using port 4200 in windows, use netstat -a -n -o | findstr 4200
    – penta
    Commented Apr 5, 2018 at 8:41
243

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
3
  • 4
    Remember to do it in cmd, not Git Bash.
    – Leo
    Commented Nov 12, 2017 at 0:30
  • 2
    Syntsax for GitBash on Windows netstat -ano | findstr :4200; taskkill -PID <establishedPID> -F
    – intotecho
    Commented Mar 7, 2018 at 2:00
  • Updating GIt Bash fix my problem Commented Mar 20, 2018 at 6:43
125

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.

2
  • 1
    Worked in Ubuntu as well. Commented Jun 25, 2019 at 10:32
  • 3
    command + C is the key to kill any process. Myself and other devs in haste press command+z, which just suspends the process rather than killing it.
    – Jasmeet
    Commented Jul 31, 2019 at 20:26
60

If you have npm installed, which I assume you have if you are using angular, then you can simply run npx kill-port 4200. That's it.

0
37

Do not use ctrl+Z, to stop the process you must use ctrl+C

0
25

Use this command to kill ng:

pkill -9 ng
1
  • 7
    i used it on my ubuntu 18.04. it flashed my screen and took me to ubuntu login killing all apps
    – srv_sud
    Commented Jan 27, 2019 at 16:04
21

When you use Ctrl+Z, you are going to suspend a running task but not to kill it. You can later use command fg to resume the task. If you want to quit ng serve, you should use Ctrl+C instead, in which will also release the 4200 port.

1
  • Not true, at least not on a mac
    – pixel
    Commented May 1, 2021 at 23:51
18

ng serve --port <YOUR_GIVEN_PORT_NUMBER>

You should try above command to run on your given port.

0
10

In summary there are more than one solution : 1 ) By using another port to define port number ,

ng serve --open --port 4201

2) by killing the process

ctrl + c // for kill
Close all node terminal which is related for running the app.

3) Type

netstat -a -n -o

in command prompt then find the related port PID and kill it by

taskkill /F /PID (pid number)

4) Type netstat -ano|findstr :4200

took the foreign address PID which contain the port number and then kill it by

taskkill /PID (pid number)/F

8

For Windows:

Open Command Prompt and

type: netstat -a -o -n

Find the PID of the process that you want to kill.

Type: taskkill /F /PID 16876

This one 16876 - is the PID for the process that I want to kill - in that case, the process is 4200 - check the attached file.you can give any port number.

enter image description here

Now, Type : ng serve to start your angular app at the same port 4200

8

I am sharing this as the fowling two commands did not do the job on my mac:

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

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

The following one did, but if you were using the integrated terminal in Visual Code, try to use your machine terminal and add the fowling command:

lsof -t -i tcp:4200 | xargs kill -9
0
8

Right now you can set --port 0 to get a free port.

ng serve --port 0 // will get a free port for you
1
  • In this case don't forget to use the --open flag, otherwise you won't know in which port was started.
    – rplaurindo
    Commented Aug 18, 2019 at 5:34
7

VScode terminal in Ubuntu OS use following command to kill process

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

Port 4200 is already in use.Use -port to specify a different port error Reasons An existing application(not angular) in your system using the port number 4200. This is a very rare scenario. In this case, you need to change the port number of angular application as mentioned below. You already ran ng serve and to exit the application you typed Control Z (Ctrl+Z), And then you typed ng serve then you will get port 4200 is already in use_ error. In this case, you need to kill the previous process.

To change the port number for our angular application use the below command

ng serve --port 4201

Now type ng serve

Our angular application will be running on http://localhost:4201

To fix port 4200 is already in use error in In Mac & Linux OS (Ubuntu etc) use the following commands

sudo kill $(sudo lsof -t -i:4200) Or
sudo kill `sudo lsof -t -i:4200` Or
sudo lsof -t -i tcp:4200 | xargs kill -9

In Window operating system open command prompt. Use the following command to fix port 4200 is already in use error.

netstat -a -n -o | findStr "4200"`

Take the process id and kill the process using the following command

taskkill -f /pid 11128
6

We can forcefully kill the port by following command.

kill -2 $(lsof -t -i:4200)
6

The most simple one line command:

 sudo fuser -k 4200/tcp
0
5
netstat -anp | grep ":4200"

This will tell you who's got the port.

2
  • is it - anp ? that returned nothing so I just added a space because your command was asking for "nestat: option requires an argument --p "
    – peztherez
    Commented Aug 23, 2016 at 3:38
  • Your netstat must be different from mine. Maybe add an OS tag.
    – Joshua
    Commented Aug 23, 2016 at 21:26
5
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>

5

With ctrl + z you put the program in the background. On Linux you can get the session back in the foreground with the following command:

fg ng serve

You don't need to kill the process.

5

To stop all the local port running in windows, use this simple comment alone instead searching for pid separatly using netstat,

It find all the pid and stop the local port which is currently running,

taskkill /im node.exe /f
0
5

With these three commands:

  1. netstat -anb | findstr 4200
  2. netstat -anbo | findstr 4200
  3. taskkill -f /pid 22416

Cmd => should be run as an administrator

First: check if it is listening or not

C:\Windows\system32>netstat -anb | findstr 4200 

Output:

TCP  127.0.0.1:4200     0.0.0.0:0      LISTENING 

Second: Find IP

C:\Windows\system32>netstat -anbo | findstr 4200 

Output:

TCP    127.0.0.1:4200     0.0.0.0:0    LISTENING    22416 

Third: kill the port

C:\Windows\system32>taskkill -f /pid 22416 

SUCCESS: The process with PID 22416 has been terminated.

You should find your own pid;

3

Just restart the IDE you are using, then it will work.

3

In my case none of the above mentioned worked.

UBUNTU 18.04 VERSION

Below command worked.

sudo kill -9 $(lsof -i tcp:4200 -t)
0
2

It says already we are running the services with port no 4200 please use another port instead of 4200. Below command is to solve the problem

ng serve --port 4300

0
2

If you are using VSCode, you have the option to kill terminal and add a new terminal. Close the tab and open a new tab. It worked for me.

Edited: Use ctrl+c and press y. With out killing terminal also, You can proceed. If you want to open a new instance of visual studio and run a different application , you can use ng serve --port 4401.

2

For me, this cmd worked Ubuntu

sudo fuser -k 4200/tcp for killing other ports
1

You can also try with this to run your application in visual studio code -:

ng serve --open --port 4201

you can give any port number.

1

It is possible to change port in .angular-cli file. For example:

"defaults": {
    "styleExt": "css",
    "component": {},
    "serve": {
      "port": 4205
    }
  }

In addition, it is necessary to add this to your package.json:

"ng": "ng",
"start": "ng serve"
1
Port 4200 is already in use. Use '--port' to specify a different port

This means that you already have another service running on port 4200. If this is the case you can either . shut down the other service. use the --port flag when running ng serve like this:

 ng serve --port 9001

Another thing to notice is that, on some machines, the domain localhost may not work. You may see a set of numbers such as 127.0.0.1. When you run ng serve it should show you what URL the server is running on, so be sure to read the messages on your machine to find your exact development URL.

1

you can use fuser -k 4200/tcp if it is Linux Operating system

Not the answer you're looking for? Browse other questions tagged or ask your own question.