Angular 6+ How to change default port 4200
Asked Answered
H

8

47

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 ?

Hospice answered 24/5, 2018 at 22:35 Comment(2)
Possible duplicate of angular-cli server - how to specify default portDeflexed
@Deflexed 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.Hospice
H
108

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

Hospice answered 24/5, 2018 at 23:29 Comment(2)
This can be set using the cli config command: ng config projects.[YOURPROJECTNAME].architect.serve.options.port 1337Quaquaversal
This is the best answer. Thanks!Marjorie
L
19

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.

Luminous answered 24/5, 2018 at 23:35 Comment(2)
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.Hospice
Makes sense, I usually have multiple projects serving with different storage/auth data so I tend to just specify when servingLuminous
P
12

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.

Punner answered 10/8, 2018 at 11:13 Comment(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!Fractional
C
5

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

Cubiform answered 10/4, 2019 at 5:0 Comment(0)
T
3

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.

Tripterous answered 31/12, 2019 at 9:41 Comment(0)
B
2

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

Barrault answered 11/12, 2018 at 6:52 Comment(0)
H
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
Heyday answered 12/5, 2023 at 9:43 Comment(0)
M
0

Best option is:

In your package.json file, "scripts": { "start": "ng serve --port 5000" }

and run this command: npm run start

Montes answered 7/9, 2023 at 6:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.