Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 6+ How to change default port 4200

This question is specific to version 6 of Angular, not earlier, since .angular-cli.json file had been replaced by angular.json file.

I created a new Angular 6 project and tried, as I always did with previous versions, to change its default port but this time in angular.json.

  "defaults": {     "serve": {       "port": 4220   },   

But get the following error:

Invalid schema detected in .angular-cli.json, please correct and try again!

Does any one know how to do this with this new version of Angular ?

like image 306
Sami-L Avatar asked May 24 '18 22:05

Sami-L


People also ask

What is the command to change the default port of Angular?

Goto nodel_modules/angular-cli/commands/server. js Search for var defaultPort = process. env. PORT || 4200; and change 4200 to anything else you want.

How do I close port 4200 on Windows?

If you want to quit ng serve , you should use Ctrl+C instead, in which will also release the 4200 port.


Video Answer


2 Answers

Due to a non accurate title: "angular-cli server - how to specify default port", it was hard to find an answer to my question, but thanks to Vladymir Gonzalez I did.

To help others find the answer quickly, I extracted here the specific part for Angular 6, belonging to elwyn :

Update for @angular/[email protected]: In the new angular.json you now specify a port per "project"

"projects": {     "my-cool-project": {         ... rest of project config omitted         "architect": {             "serve": {                 "options": {                     "port": 4850    <-- add your custom port number here                       }             }         }     } } 

All options available:

https://github.com/angular/angular-cli/wiki/angular-workspace

like image 53
Sami-L Avatar answered Sep 22 '22 07:09

Sami-L


You can always specify the port while serving also: ng serve --port 3000

You can put any valid port number there and it will serve from that port.

like image 32
Nicholas Pesa Avatar answered Sep 23 '22 07:09

Nicholas Pesa