Reputation: 4031
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.
Upvotes: 309
Views: 621899
Reputation: 973
A one-line on macOS would be to kill the port, thanks to Claudio Holanda, and then reinitiate it again:
npx kill-port 4200 && ng serve -o
It still requires CMD + Z to suspend ng serve.
Upvotes: 1
Reputation: 153
For me, this cmd worked Ubuntu
sudo fuser -k 4200/tcp for killing other ports
Upvotes: 2
Reputation: 1185
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.
Upvotes: 68
Reputation: 91
With these three commands:
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;
Upvotes: 5
Reputation: 45
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.
Upvotes: 0
Reputation: 2004
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
Upvotes: 8
Reputation: 103
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.
Upvotes: 1
Reputation: 494
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
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
Upvotes: 0
Reputation: 1737
VScode terminal in Ubuntu OS use following command to kill process
lsof -t -i tcp:4200 | xargs kill -9
Upvotes: 7
Reputation: 703
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.
Upvotes: 0
Reputation:
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
Upvotes: 1
Reputation: 6164
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.
Upvotes: 523
Reputation: 1
To fix this issue in Windows OS, open Task Manager, and terminate the process.
Upvotes: 0
Reputation: 629
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.
Upvotes: 0
Reputation: 8692
Right now you can set --port 0
to get a free port.
ng serve --port 0 // will get a free port for you
Upvotes: 8
Reputation: 272
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.
Upvotes: 2
Reputation: 15908
In my case none of the above mentioned worked.
UBUNTU 18.04 VERSION
Below command worked.
sudo kill -9 $(lsof -i tcp:4200 -t)
Upvotes: 3
Reputation: 457
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.
Upvotes: 0
Reputation: 1149
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
Upvotes: 5
Reputation: 145
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
Upvotes: 1
Reputation: 121
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
Upvotes: 8
Reputation: 660
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.
Upvotes: 5
Reputation: 841
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
Upvotes: 10
Reputation: 9303
To Access project outside localhost
Example:
ng serve --host 192.168.2.2:7006
Upvotes: -1
Reputation: 243
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)
.
Upvotes: 1
Reputation: 1215
Only thing that works for me is to restart my system / device...
Using Webstorm IDE by JetBrains.
Upvotes: 0
Reputation: 9961
ng serve --port <YOUR_GIVEN_PORT_NUMBER>
You should try above command to run on your given port.
Upvotes: 19