Sami-L
Sami-L

Reputation: 5705

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 ?

Upvotes: 48

Views: 80188

Answers (8)

Bittu
Bittu

Reputation: 1

Best option is:

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

and run this command: npm run start

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

Upvotes: 0

Sami-L
Sami-L

Reputation: 5705

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

Upvotes: 113

Satyam Anand
Satyam Anand

Reputation: 507

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.

Upvotes: 3

Deva
Deva

Reputation: 2121

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.

Upvotes: 12

Harish Mahajan
Harish Mahajan

Reputation: 3294

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

Upvotes: 5

Abhishek
Abhishek

Reputation: 1778

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

Upvotes: 2

Nicholas Pesa
Nicholas Pesa

Reputation: 2196

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.

Upvotes: 19

Related Questions