Angular-cli: Feature request: ng serve should select another port if 4200 is already taken

Created on 5 Aug 2016  Â·  16Comments  Â·  Source: angular/angular-cli

Feature request

  • OS?
    Windows 7
  • Versions.
angular-cli: 1.0.0-beta.11-webpack
node: 4.4.7
os: win32 x64
  • Repro steps.

a. start an angular project with ng serve
b. start another angular project with ng serve

The second one won't start as port 4200 is already taken
Port 4200 is already in use.

Ionic cli is switching to another port automatically
The port 8100 was taken on the host localhost - using port 8101 instead

3 (nice to have) feature

Most helpful comment

There is currently a way to get an free port, by using --port=0:

$ ng serve --port=0
** NG Live Development Server is running on http://localhost:49152. **
# and on another console
$ ng serve --port=0
** NG Live Development Server is running on http://localhost:49153. **

Thanks to @clydin for highlighting this.

We really need to document these things better, @Puigcerber was working on a fix and it turned out we already had a functionality and no one remembered :/

All 16 comments

Random ports are a bad idea, especially if you use callbacks to login (like facebook). Just provide an easy option in the config (not the ember config) to specify the port.

I don't think random ports are a good idea as well. I wanted angular-cli to do like ionic-cli. increment the port number until the first available port is found and printed.

I'd rather have cli configured with my port, otherwise the port of my app would depend on in which order I start my two projects.

@bhelzer You already have the option to specify the port at your wish

ng serve --host 0.0.0.0 --port 4201 --live-reload-port 49153

We are proposing the facility of automatically switch to another port if the port is already taken.

There is currently a way to get an free port, by using --port=0:

$ ng serve --port=0
** NG Live Development Server is running on http://localhost:49152. **
# and on another console
$ ng serve --port=0
** NG Live Development Server is running on http://localhost:49153. **

Thanks to @clydin for highlighting this.

We really need to document these things better, @Puigcerber was working on a fix and it turned out we already had a functionality and no one remembered :/

Thanks @amreladawy ng serve --host 0.0.0.0 --port 4201 --live-reload-port 49153
works for me.

@filipesilva
Could there be a way to opt into killing the task that is running automatically instead of getting the error? More and more I run into the issue that I closed the window running the app (VSCODE/Powershell/CommandPrompt) or it was closed for me. Then when I go to open it again and run npm start I get Port 4200 is already in use error.

I used to go into task manager and try to find the correct nodejs.exe and end the process. Now I run this
netstat -ano | findstr :4200 | ConvertFrom-String | ForEach-Object {taskkill /PID $_.P6 /F}
But it would be nice that if something is already running on that port to just kill it and then run as normal.

Normally I would just switch to another port but we are using json webtokens and Identity Server for login so the local url has to match exactly.

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.

@filipesilva thanks for your suggestion (--port 0) server is starting on the opened port, but we are facing issue with e2e target with --port 0 application is running on opened port but base url is http://localhost:0.

@filipesilva I am new to angular and was facing the following problem:
I do ng serve in cmd prompt and then the angular application starts in browser at localhost 4200.
Now if i close the cmd prompt ,(i would hope that application should close) even then the application keep on running at localhost 4200. If I have to run another angular application
In order to run that other project ,what i would have to do is kill the process running on port 4200 by windows commands which becomes pain when we are developing in angular and have to do that several times(considering one closed the cmd window after doing ng serve).

is there any solution to that apart from ng serve --port?

Try Ctrl + C in the command prompt

On Wed, Jul 25, 2018 at 8:45 AM PawanAtCentennial notifications@github.com
wrote:

@filipesilva https://github.com/filipesilva I am new to angular and was
facing the following problem:
I do ng serve in cmd prompt and then the angular application starts in
browser at localhost 4200.
Now if i close the cmd prompt ,(i would hope that application should
close) even then the application keep on running at localhost 4200. If I
have to run another angular application
In order to run that other project ,what i would have to do is kill the
process running on port 4200 by windows commands which becomes pain when we
are developing in angular and have to do that several times(considering one
closed the cmd window after doing ng serve).

is there any solution to that apart from ng serve --port?

—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
https://github.com/angular/angular-cli/issues/1568#issuecomment-407758979,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AK2gEo9nHfEgolXTtC-AjzQw_-zdC9W-ks5uKHZtgaJpZM4Jd_Jb
.

pain0486 i mean if you close the command promt by mistake

like actually closing the window

Ahhh, nope then you just stuck or use the --port=0 like mentioned above, could also try this in powershell
netstat -ano | findstr :4200 | ConvertFrom-String | ForEach-Object {taskkill /PID $_.P6 /F}

@pain0486 oh ok ........ I thought it would be similar to node .... like i we close the window then the server automatically stops ................. ok thanks btw and what would you reccommend for getting expert in angular2+

Read the docs also if you have access to something like pluralsight would be good. but mainly just start a project and everytime you have a question or design question look at the docs and stackoverflow. That is where I did 95% of my learning. Pluralsight is easy to consume but I didnt have the time to really watch the videos.

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

_This action has been performed automatically by a bot._

Was this page helpful?
0 / 5 - 0 ratings

Related issues

IngvarKofoed picture IngvarKofoed  Â·  3Comments

gotschmarcel picture gotschmarcel  Â·  3Comments

rajjejosefsson picture rajjejosefsson  Â·  3Comments

rwillmer picture rwillmer  Â·  3Comments

ericel picture ericel  Â·  3Comments