45

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 ?

2
  • 3
    Possible duplicate of angular-cli server - how to specify default port
    – Orlandster
    May 24, 2018 at 22:39
  • 1
    @Orlandster the mentioned question has been emitted on May 11 '16, it was talking about angular-cli, version 6 of Angular was issued on May '18, it's confusing to find such answer there.
    – Sami-L
    May 25, 2018 at 0:05

7 Answers 7

106

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/cli@6.x: 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

2
  • 6
    This can be set using the cli config command: ng config projects.[YOURPROJECTNAME].architect.serve.options.port 1337
    – Rob
    Jul 2, 2018 at 18:17
  • 1
    This is the best answer. Thanks! Feb 4, 2020 at 3:27
18

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.

2
  • 1
    I prefer to avoid this by saving the port in angular.json file, this way I can use only "ng serve -o" without trying to remember each time the port number for each app.
    – Sami-L
    May 24, 2018 at 23:44
  • Makes sense, I usually have multiple projects serving with different storage/auth data so I tend to just specify when serving May 24, 2018 at 23:45
11

I am giving answer for angular 2+ application.

If you wanted to start 2 angular application in different port i.e.

1) port 4200 
2) port 5000

you need to change "package.json" file and just change add one line "Script block" in first curly braces where your application name, version is default available,

"scripts": {
    "ng": "ng",
    "start": "ng serve --port 5000 ",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  }, 

Start your application by "npm start" command. This command will start your application on Port 5000.

1
  • 1
    The best answer for long-term use so I would not have to use "ng serve --port 4300" (or another number) every time. Thanks!
    – Dacomis
    Sep 4, 2019 at 18:40
4

The answer is simple write ng serve --open --port=YourPortNumber in command prompt.

Example :

ng serve --open --port=4201

==== OTHER SOLUTION ====

You can also change it in package.json

"scripts": {
  "start": "ng serve --open --port=4201",
}

Now, in command propmt just write npm start

2

To run angular on a specified port use command sudo ng serve --port 8080 in place of 8080 you can specify any port number.

This will only run the project on the port number unless you close it.

1

Two way to change your default port First: using command

ng serve --port 8000 || ng serve --host '192.168.1.1' --port 8000

Second: edit your package.json

"scripts": {
    "ng": "ng",
    "start": "ng serve --port 8000",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  }, 

If you are using multiple project in some time in your system that condition first one is best and i thing first is good for practice

0

In case you are running more than one angular project, you need to run the application in a different port.

By default port number: 4200

Port number range - 1024 to 65535

while running the project you can mention the specific port number

Ex : ng serve –open –port=5200

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.