angular-cli icon indicating copy to clipboard operation
angular-cli copied to clipboard

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

Open amreladawy opened this issue 10 years ago • 16 comments

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

amreladawy avatar Aug 05 '16 19:08 amreladawy

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.

mwawrusch avatar Sep 01 '16 16:09 mwawrusch

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.

amreladawy avatar Sep 02 '16 19:09 amreladawy

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 avatar Nov 22 '16 22:11 bhelzer

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

amreladawy avatar Nov 23 '16 04:11 amreladawy

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 :/

filipesilva avatar Jan 27 '17 10:01 filipesilva

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

manoj30dec avatar May 24 '17 05:05 manoj30dec

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

pain0486 avatar Aug 29 '17 19:08 pain0486

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.

harmeshg5 avatar Jan 04 '18 06:01 harmeshg5

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

Ram569 avatar Jul 09 '18 09:07 Ram569

@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?

PawanAtCentennial avatar Jul 25 '18 13:07 PawanAtCentennial

Try Ctrl + C in the command prompt

On Wed, Jul 25, 2018 at 8:45 AM PawanAtCentennial [email protected] 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 avatar Jul 25 '18 13:07 pain0486

pain0486 i mean if you close the command promt by mistake

like actually closing the window

PawanAtCentennial avatar Jul 25 '18 13:07 PawanAtCentennial

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 avatar Jul 25 '18 13:07 pain0486

@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+

PawanAtCentennial avatar Jul 25 '18 13:07 PawanAtCentennial

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.

pain0486 avatar Jul 25 '18 13:07 pain0486

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.