288

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.

peztherez
  • 3,751
  • 5
  • 20
  • 21
  • 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 – Kildareflare Sep 07 '19 at 01:04
  • 2
    If you are working on visual studio code, the close it and restart it. – Kurkula Jun 23 '20 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 :) – Rebai Ahmed Jul 13 '21 at 23:44
  • 8
    `npx kill-port 4200` – Claudio Holanda Sep 23 '21 at 14:00

51 Answers51

489

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.

Bart Hoekstra
  • 5,814
  • 1
  • 15
  • 13
  • 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? – Nathan Dunn May 02 '17 at 04:49
  • 6
    You can specify a different port with: `--port ` – hestellezg Jun 10 '17 at 05:07
  • 4
    This kills the default browser. And not helpful. – shijin Jun 20 '17 at 13:08
  • Please specify the environment required to use those commands – usefulBee Jul 12 '17 at 19:40
  • 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! – crackpotHouseplant Jul 25 '17 at 01:56
  • 2
    To quickly check which pid is using port 4200 in windows, use netstat -a -n -o | findstr 4200 – penta Apr 05 '18 at 08:41
  • For windows users: You need execute the terminal as administrator. – lorenago Mar 13 '19 at 10:07
  • 1
    lsof -t -i tcp:4200 | xargs kill -9 ----- Try this command in the terminal app rather then on VSCode terminal. It worked for me. – gaurish.salunke Mar 17 '19 at 06:11
  • This is not working either on ubuntu 18 . This however works fuser -k 4200/tcp taken from this : https://github.com/angular/angular-cli/issues/8734 – Mark Odey Sep 16 '19 at 18:01
  • This also worked for me https://anthonygiretti.com/2018/03/26/how-to-avoid-port-4200-is-already-in-use-error-with-angular-cli/ – pradeepradyumna Jul 04 '20 at 12:09
  • "sudo kill `sudo lsof -t -i:4200`" works for ubuntu. If your command promt did not allow you to make changes in **/bin/su** as a user previlge, then switch to root user by using command **sudo su** and provide the prompted password and try again. – GSangram Jul 10 '20 at 18:33
  • 1
    This doesn't work for me, I have to use `sudo kill -9 $(sudo lsof -t -i:4200)` on my Ubuntu machine. – Jason Liu Dec 29 '20 at 00:50
  • for windows users one liner found in another answer in this page for /f "tokens=5" %a in ('netstat -ano ^| find "4200" ^| find "LISTENING"') do taskkill /f /pid %a – Sundara Prabu May 16 '21 at 19:39
  • One windows you run this command on powershell (admin mode): Get-NetTCPConnection -State Listen | where { $_.LocalPort -eq 4200 } | % { stop-process -Id $_.OwningProcess } – Youssef Bouha May 10 '22 at 13:27
237

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
Anthony Kal
  • 2,729
  • 1
  • 20
  • 18
123

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.

Julian Fraser
  • 1,398
  • 1
  • 9
  • 7
43

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.

Kapilfreeman
  • 897
  • 9
  • 10
35

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

Mike Kormendy
  • 3,389
  • 2
  • 24
  • 21
Emir Mamashov
  • 1,108
  • 9
  • 14
25

Use this command to kill ng:

pkill -9 ng
Lolita
  • 267
  • 3
  • 2
  • 7
    i used it on my ubuntu 18.04. it flashed my screen and took me to ubuntu login killing all apps – srv_sud Jan 27 '19 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.

Roman Lo
  • 211
  • 2
  • 4
17

ng serve --port <YOUR_GIVEN_PORT_NUMBER>

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

Shubham Verma
  • 8,783
  • 6
  • 58
  • 79
9

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

Mohaimin Moin
  • 821
  • 11
  • 22
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

Jay Kareliya
  • 760
  • 7
  • 21
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
Ahmed Hadi
  • 121
  • 1
  • 3
  • 12
8

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

ng serve --port 0 // will get a free port for you
Yevhen Laichenkov
  • 7,746
  • 2
  • 27
  • 33
  • In this case don't forget to use the ```--open``` flag, otherwise you won't know in which port was started. – rplaurindo Aug 18 '19 at 05:34
7

VScode terminal in Ubuntu OS use following command to kill process

lsof -t -i tcp:4200 | xargs kill -9
Surendranath Sonawane
  • 1,595
  • 1
  • 18
  • 24
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
Kavinda Senarathne
  • 1,813
  • 13
  • 15
6
netstat -anp | grep ":4200"

This will tell you who's got the port.

Joshua
  • 40,822
  • 8
  • 72
  • 132
  • 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 Aug 23 '16 at 03:38
  • Your netstat must be different from mine. Maybe add an OS tag. – Joshua Aug 23 '16 at 21:26
6

We can forcefully kill the port by following command.

kill -2 $(lsof -t -i:4200)
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
6

The most simple one line command:

 sudo fuser -k 4200/tcp
Salman Ahmed
  • 682
  • 1
  • 10
  • 24
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>

shijin
  • 2,998
  • 1
  • 24
  • 30
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.

Spirit
  • 631
  • 7
  • 11
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
saravana va
  • 1,081
  • 1
  • 13
  • 17
4

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;

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
3

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

amc software
  • 747
  • 1
  • 7
  • 18
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)
Alien
  • 15,141
  • 6
  • 37
  • 57
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

rollstuhlfahrer
  • 3,988
  • 9
  • 25
  • 38
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
abduljalil
  • 115
  • 1
  • 2
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"
StepUp
  • 36,391
  • 15
  • 88
  • 148
1

Instead of killing the whole process or using ctrl+z, you can simply use ctrl+c to stop the server and can happily use ng serve command without any errors or if you want to run on a different port simply use this command ng serve --port portno(ex: ng serve --port 4201).

Alekya
  • 239
  • 5
  • 15
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.

Mohammad Daliri
  • 1,370
  • 6
  • 20
  • 43
1

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

1

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

Georgi Georgiev
  • 362
  • 6
  • 9
1

For Ubndu 18.04 sudo lsof -t -i tcp:3000 | xargs kill -9

Its happen when port was unsucessfully terminated so this command will terminat it 4200 or 3000 or3300 any

user2642281
  • 125
  • 1
  • 4
1

It seems like another program is using your default port 4200. Good thing is, ports above 4200 are usually free. Just pick another one. For example:

ng serve --port 4210
1

On my Ubuntu i had typo crl+z to close terminal and after that i couldn't acces localhost:4200. The only solutin was to do:

$ sudo fuser -k 4200/tcp

and after that restart the server.

JoBaHP
  • 99
  • 1
  • 3
0

I also faced the same error msg, so i tried ng serve --port 12012 and it worked fine.

Sachin Gaikwad
  • 331
  • 3
  • 10
0

If you compiling your angular JS code in both CMD and IDE then this issue occur. In CMD, your angular JS code compile automatically whenever you change your angular JS code in IDE and then your IDE want to occupy the same port i.e 4200 which is occupied by CMD already So, there is a simple solution for this issue, just close your cmd while compiling your code in IDE.

vishal
  • 1
0

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

Ismail Farooq
  • 6,309
  • 1
  • 27
  • 47
0

On linux mint 17, this is working.

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

Apar
  • 111
  • 1
  • 8
0

netstat -plnet

tcp 0 0 127.0.0.1:4200 0.0.0.0:* LISTEN 1001 63955 7077/ng

kill -9 7077

again start your ng serve.

  • 1
    While this code snippet may solve the question, [including an explanation](//meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. Please also try not to crowd your code with explanatory comments, this reduces the readability of both the code and the explanations! – Filnor Jun 08 '18 at 06:06
0

In Windows; In command prompt which is running your ng serve just click and press keys:

ctrl + c

It will ask you: Terminate batch job (Y/N)? and you type Y

Sharif Yazdian
  • 4,332
  • 3
  • 20
  • 22
  • Why doesn't this work in VSCode? I've been typing ctrl + c and all it does repeat the directory path prompt. Thanks. – Chris22 Nov 24 '20 at 00:07
  • @Chris22 first click on the bottom of VScode, which is the command area, and make that area selected, then go with ctr + c – Sharif Yazdian Nov 24 '20 at 04:34
  • I am typing CTRL + C in the VSCode terminal, but that is not quitting the server running on port 4200. It just duplicates the prompt. – Chris22 Nov 24 '20 at 18:23
0

As first step after any changes, I use ng build, then ng serve. It works without any problems.

0

Only thing that works for me is to restart my system / device...

Using Webstorm IDE by JetBrains.

Rick
  • 1,177
  • 1
  • 19
  • 27
0

I too faced similar problem and problem was basically that I had opened multiple running terminals in powershell. So make sure you Ctrl+c all others.

il_raffa
  • 5,090
  • 129
  • 31
  • 36
Priyanka Arora
  • 409
  • 4
  • 10
0

I had big troubles with this, and I was even trying with many others ports and nothing.

I did npm install typescript@2.8 and it solved the problem. For some reason the typescript version was causing a conflict and using yarn or ng serve were not working.

hope this works for anyone else with the same issue.

Alejandro Giraldo
  • 609
  • 1
  • 6
  • 11
0

To fix this issue in Windows OS, open Task Manager, and terminate the process.

TaskManager

0

close the command prompt (cmd) then use text editor terminal or use only cmd to run commands.

in my case this is the problem and how i solved it.

most of the time you are created angular app using command prompt. then you trying to run the app terminal in inside of the text editor. whenever you trying to save you can see some stuff running on the windows command prompt. so just close it and run the command in the terminal of the text editor.

HeshanHH
  • 653
  • 7
  • 9
0

For windows users:

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

netstat -a -n -o

enter image description here

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

step 2.command in cmd:

taskkill /F /PID 6500

step 3: Now, Type :

ng serve to start your angular app at the same port 4200

0
ng serve --open 

assigns a local host to host the project.On running the same command in same directory the system assigns localhost:port where port is already in use. To override this issue one can easily run the following command:

ng serve --port

this will assign a new port for the project without disturbing the present local host port.

DaveL17
  • 1,673
  • 7
  • 24
  • 38
  • 1
    This answer was already given. Please check others' answer first before posting duplicate answers – Vega Jun 12 '21 at 08:41
-1

ng serve --port 122345 is worked fine for me.

Radi
  • 41
  • 1
  • 4
-1

To Access project outside localhost

Example:

ng serve --host 192.168.2.2:7006
Dibish
  • 9,133
  • 22
  • 64
  • 106