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

Angular

Angular Problem Overview


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.

Angular Solutions


Solution 1 - Angular

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 - Angular

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 - Angular

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.

Solution 4 - Angular

Not ctrl+Z, you must run for stop ctrl+C

Solution 5 - Angular

Use this command to kill ng:

pkill -9 ng

Solution 6 - Angular

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.

Solution 7 - Angular

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.

Solution 8 - Angular

ng serve --port <YOUR_GIVEN_PORT_NUMBER>

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

Solution 9 - Angular

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)

  1. 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

Solution 10 - Angular

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

Solution 11 - Angular

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

ng serve --port 0 // will get a free port for you

Solution 12 - Angular

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

Solution 13 - Angular

netstat -anp | grep ":4200"

This will tell you who's got the port.

Solution 14 - Angular

We can forcefully kill the port by following command.

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

Solution 15 - Angular

The most simple one line command:

 sudo fuser -k 4200/tcp

Solution 16 - Angular

VScode terminal in Ubuntu OS use following command to kill process

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

Solution 17 - Angular

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>

Solution 18 - Angular

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

Solution 19 - Angular

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.

Solution 20 - Angular

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

Solution 21 - Angular

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

Solution 22 - Angular

In my case none of the above mentioned worked.

UBUNTU 18.04 VERSION

Below command worked.

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

Solution 23 - Angular

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

Solution 24 - Angular

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.

Solution 25 - Angular

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;

Solution 26 - Angular

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.

Solution 27 - Angular

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).

Solution 28 - Angular

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

Solution 29 - Angular

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

Solution 30 - Angular

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

Solution 31 - Angular

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

Solution 32 - Angular

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.

Solution 33 - Angular

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

Solution 34 - Angular

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.

Solution 35 - Angular

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

Solution 36 - Angular

On linux mint 17, this is working.

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

Solution 37 - Angular

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"

Solution 38 - Angular

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.

Solution 39 - Angular

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.

Solution 40 - Angular

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

Solution 41 - Angular

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

Solution 42 - Angular

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

Using Webstorm IDE by JetBrains.

Solution 43 - Angular

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.

Solution 44 - Angular

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

I did npm install [email protected] 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.

Solution 45 - Angular

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

TaskManager

Solution 46 - Angular

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.

Solution 47 - Angular

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

Solution 48 - Angular

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.

Solution 49 - Angular

For me, this cmd worked Ubuntu

sudo fuser -k 4200/tcp for killing other ports

Solution 50 - Angular

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

Solution 51 - Angular

To Access project outside localhost

Example:

ng serve --host 192.168.2.2:7006

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionpeztherezView Question on Stackoverflow
Solution 1 - AngularBart HoekstraView Answer on Stackoverflow
Solution 2 - AngularAnthony KalView Answer on Stackoverflow
Solution 3 - AngularJulian FraserView Answer on Stackoverflow
Solution 4 - AngularEmir MamashovView Answer on Stackoverflow
Solution 5 - AngularLolitaView Answer on Stackoverflow
Solution 6 - AngularRoman LoView Answer on Stackoverflow
Solution 7 - AngularKapilfreemanView Answer on Stackoverflow
Solution 8 - AngularShubham VermaView Answer on Stackoverflow
Solution 9 - AngularMohaimin MoinView Answer on Stackoverflow
Solution 10 - AngularJay KareliyaView Answer on Stackoverflow
Solution 11 - AngularYevhen LaichenkovView Answer on Stackoverflow
Solution 12 - AngularAhmed HadiView Answer on Stackoverflow
Solution 13 - AngularJoshuaView Answer on Stackoverflow
Solution 14 - Angularsharath parupatiView Answer on Stackoverflow
Solution 15 - AngularSalman AhmedView Answer on Stackoverflow
Solution 16 - AngularSurendranath SonawaneView Answer on Stackoverflow
Solution 17 - AngularshijinView Answer on Stackoverflow
Solution 18 - Angularsaravana vaView Answer on Stackoverflow
Solution 19 - AngularSpiritView Answer on Stackoverflow
Solution 20 - AngularKavinda SenarathneView Answer on Stackoverflow
Solution 21 - Angularamc softwareView Answer on Stackoverflow
Solution 22 - AngularAlienView Answer on Stackoverflow
Solution 23 - Angulardharma rao PalliView Answer on Stackoverflow
Solution 24 - AngularDIBYA RANJAN TRIPATHYView Answer on Stackoverflow
Solution 25 - AngularBaran GoodarziView Answer on Stackoverflow
Solution 26 - AngularHarmesH KaushiKView Answer on Stackoverflow
Solution 27 - AngularAlekyaView Answer on Stackoverflow
Solution 28 - AngularMachhindra NeupaneView Answer on Stackoverflow
Solution 29 - AngularGeorgi GeorgievView Answer on Stackoverflow
Solution 30 - Angularuser2642281View Answer on Stackoverflow
Solution 31 - Angularuser12465043View Answer on Stackoverflow
Solution 32 - AngularJoBaHPView Answer on Stackoverflow
Solution 33 - AngularSachin GaikwadView Answer on Stackoverflow
Solution 34 - AngularvishalView Answer on Stackoverflow
Solution 35 - AngularIsmail FarooqView Answer on Stackoverflow
Solution 36 - AngularzerreView Answer on Stackoverflow
Solution 37 - AngularStepUpView Answer on Stackoverflow
Solution 38 - AngularMohammad DaliriView Answer on Stackoverflow
Solution 39 - AngularHacker JarvisView Answer on Stackoverflow
Solution 40 - AngularSharif YazdianView Answer on Stackoverflow
Solution 41 - AngularLena NeumannView Answer on Stackoverflow
Solution 42 - AngularRickView Answer on Stackoverflow
Solution 43 - AngularPriyanka AroraView Answer on Stackoverflow
Solution 44 - AngularAlejandro GiraldoView Answer on Stackoverflow
Solution 45 - AngularKranthi GolamariView Answer on Stackoverflow
Solution 46 - AngularHeshanHHView Answer on Stackoverflow
Solution 47 - AngularSoft Dev Ahmad yar khanView Answer on Stackoverflow
Solution 48 - Angularvikashjha.tpView Answer on Stackoverflow
Solution 49 - AngularabduljalilView Answer on Stackoverflow
Solution 50 - AngularRadiView Answer on Stackoverflow
Solution 51 - AngularDibishView Answer on Stackoverflow