ChatGPT解决这个技术问题 Extra ChatGPT

How to change angular port from 4200 to any other

I want to use 5000 instead of 4200.

I have tried to create a file on root name ember-cli and put JSON according to the code below:

{
   "port": 5000
}

But my app still runs on 4200 instead of 5000

Did u also Restart the server?
yes @kristjanreinhold
Does this answer your question? angular-cli server - how to specify default port

D
David

The solution worked for me was

ng serve --port 4401    

(You can change 4401 to whatever number you want)

Then launch browser -> http://localhost:4401/

Basically, I was having two Applications and with the help of the above approach now I am able to run both of them simultaneously in my development environment.


sidenote for others: don't do npm start --port 4401 because npm start doesn't seem to execute the --port
just an addition, you can at the same time open the served app in a new browser tab by adding ng serve --port 4401 --open
in package.json, look for ng serve command. Replace ng serve command as ng serve --port 5000. Then in terminal use npm start. It should work
N
Nathan Friend

It seems things have changed in recent versions of the CLI (I'm using 6.0.1). I was able to change the default port used by ng serve by adding a port option to my project's angular.json:

{
    "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
    "projects": {
        "my-project": {
            "architect": {
                "serve": {
                    "options": {
                        "port": 4201
                    }
                }
            }
        }
    }
}

(Only relevant properties are shown in this example.)


And i want to change host also then
A much better answer as you don't have to worry about remembering which port to use if you have different projects open.
g
ganesh kalje

You can change your application port by entering the following command,

ng serve --port 4200

Also, for a permanent setup you can change the port by editing angular-cli.json file

 "defaults": {
    "serve": {
      "port": 8080
    }
  }

In newer releases you should use ng serve --port 8080 (no ":", blank space instead).
If this is the last "defaults" setting, you should get rid of the trailing comma
This has changed in recent versions of the CLI - see my answer for more details.
D
Dhyey

Changing nodel_modules/angular-cli/commands/server.js is a bad idea as it will be updated when you install new version of angular-cli. Instead you should specify ng serve --port 5000 in package.json like this:

"scripts": {
    "start": "ng serve --port 5000"
}

You can also specify host with --host 127.0.0.1


C
Community

The location for port settings has changed a couple of times.

If using Angular CLI 1

Change angular-cli.json

{
  "defaults": {
    "serve": {
      "host": "0.0.0.0",
      "port": 5000 
    }
  }
}

If using the latest Angular CLI

Change angular.json

"projects": {
    "project-name": {
        ...
        "architect": {
            "serve": {
                "options": {
                  "host": "0.0.0.0",
                  "port": 5000
                }
            }
        }
        ...
    }
}

Without changing any file

Run the command

ng serve --host 0.0.0.0 --port 5000

This should be considered as the correct answer as it covers both possibilities: permanent or temporary solutions.
In case of permanent changes, do you know if the new port should be specified elsewhere in this ` angular.json` file ? My serve option level contains two items with browserTarget, options and configurations/production. Should I add this new port entry in both of those or just the options one ?
Any thought on this warning - or whatever you call it - that popup in the terminal? Binding this server to an open connection can result in compromising your application or computer. Using a different host than the one passed to the "--host" flag might result in websocket connection issues. You might need to use "--disable-host-check" if that's the case.
S
Sajeetharan

No one has updated answer for latest Angular CLI.With latest Angular CLI

With latest version of angular-cli in which angular-cli.json renamed to angular.json , you can change the port by editing angular.json file you now specify a port per "project"

projects": {
    "my-cool-project": {
        ... rest of project config omitted
        "architect": {
            "serve": {
                "options": {
                    "port": 4500
                }
            }
        }
    }
}

Read more about it here


c
csikosjanos

I usually use the ng set command to change the Angular CLI settings for project level.

ng set defaults.serve.port=4201

It changes change your .angular.cli.json and adds the port settings as it mentioned earlier.

After this change you can use simply ng serve and it going to use the prefered port without the need of specifying it every time.


get/set appears to be deprecated in favor of config in 6.0, so this no longer works.
A
Ajitesh Jaiswal

you can also enter the below command in your angular cli where you normally enter npm start

ng serve --host "ip-address" --port "port-number"


s
sid7747

To change port for once while running and no change in configuration you can use below command

ng serve --port 5000

If for every run of ng serve you need to have 5000 port. Do with below methods

Using angular.json

https://i.stack.imgur.com/jyYaR.png

In node_modules > @angular-devkit > build-angular > src > dev-server > schema.json, you will find below code and update port as you want like 5000. In package.json under "scripts" we've "start" update it with below command so on each run of "npm start" it will serve on 5000.

Note: Post all that restart server in terminal with 'ng serve' or 'npm start' Feedback helps to improve. Thanks!


S
Shyam

It is not recommended to change in node modules as updates or re-installation may remove those changes. So better to change in angular cli

Angular 9 in angular.json

{
    "projects": {
            "targets": {
                "serve": {
                    "options": {
                         "port": 5000
                     }
                }     
            }     
    }
}  

S
Sara Vaseei

you can also write this command: ng serve -p 5000


D
Dor Levi

For angular 10+:

Navigate to angular.json file. Search for the "serve" field. Under "serve" it will be the "options" field (if not exist add the options field). Add this line to "options": "port": 4333(any port that you want).

Example:

        "serve": {
      "builder": "@angular-devkit/build-angular:dev-server",
      "options": {
        "port": 4333
      },
      "configurations": {
        "production": {
          "browserTarget": "dashboard:build:production"
        }
      }
    },

D
Deepak swain

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

To Run Now: ng serve --port 4500 (You an change 4500 to any number you want to use as your port)


I don't think changing the contents of a package installed locally on node_modules is a good practice.
S
Sohail Ahmad

goto this file

node_modules\@angular-devkit\build-angular\src\dev-server\schema.json

and search port

"port": {
  "type": "number",
  "description": "Port to listen on.",
  "default": 4200
},

change default value whatever you want and restart the server


Works for me, just need to notice to change the mentioned file and not the other-node_modules/@angular/cli/lib/config/schema.json that contain the same property .
Generally, do not mess with node_modules! Although this solution will work temporarily, the changes will be lost as soon as @angular/devkit/build-angular gets an update
m
manikandan

Simply set in package.json file

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

then run the command

npm start

C
Chris

In angular.json:

"serve": {
          "builder": "@angular-devkit/build-angular:dev-server",
          "options": {
          "browserTarget": "projectname:build",
          "port": 5000
         }

I am using angular-cli. This worked for me.


x
xinthose

Go to angular.json file,

Search for keyword "serve":

Add port keyword as shown below:

 "serve": {
     "builder": "@angular-devkit/build-angular:dev-server",
     "options": {
         "browserTarget": "my-new-angular-app:build",
         "port": 3001
     },
     ...

S
S J BHAVANA

ways to change the port number in an angular project:

node_modules \ @angular-devkit\build-angular\src\dev-server\schema.json in this path

    "port": {
             "type": "number",
             "description": "Port to listen on.",
             "default": <<port number>>     /* write your required port number here */
   } 

Running the project using

    ng serve --port <<port number>> --open

In angular.json file

   "server": {
          "builder": "@angular-devkit/build-angular:dev-server",
          "options": {
          "browserTarget": "projectname:build",
          "port": 5000   /* add this part */
         }
adding this part and starting the server.

t
themightyhulk

Although there are already numerous valid solutions in the above answers, here is a visual guide and specific solution for Angular 7 projects (possibly earlier versions as well, no guarantees though) using Visual Studio Code. Locate the schema.json in the directories as shown in the image tree and alter the integer under port --> default for a permanent change of port in the browser.

https://i.stack.imgur.com/oOtuY.jpg


@s34N Glad to hear that. If the answer was of help, please remember to give an upvote on the post if not already done. Thanks mate.
K
Klaus Klein

If you have more than one enviroment, you may add the following way in the angular.json:

 "serve": {
      "builder": "@angular-devkit/build-angular:dev-server",
      "options": {
        "browserTarget": "your_project:build"
      },
      "configurations": {
        "production": {
          "browserTarget": "your_project:build:production"
        },
        "es5": {
          "browserTarget": "your_project:build:es5"
        },
        "local": {
          "browserTarget": "your_project:build:local",
          "port": 4201
        },
        "uat": {
          "browserTarget": "your_project:build:uat"
        }
      }
    },

This way only the local points to another port.


M
Matthieu Brucher

We have two ways to change default port number in Angular.

First way to cli command:

ng serve --port 2400 --open

Second way is by configuration at the location: ProjectName\node_modules\@angular-devkit\build-angular\src\dev-server\schema.json.

Make changes in schema.json file.

{
 "title": "Dev Server Target",
  "description": "Dev Server target options for Build Facade.",
  "type": "object",
  "properties": {
    "browserTarget": {
      "type": "string",
      "description": "Target to serve."
    },
    "port": {
      "type": "number",
      "description": "Port to listen on.",
      "default": 2400
    },

It's not recommended to change files in node_modules that might be overwritten after reinstalling the package. A very good solution were presented by Sajeetharan.
d
dhilt

If you want to use NodeJS environment variable to set up the value of the port of Angular CLI dev server, following approach is possible:

create NodeJS script, which will have an access to the environment variable (say, DEV_SERVER_PORT) and could run ng serve with --port parameter value taken from that variable (child_process might be our friend here):

const child_process = require('child_process');
const child = child_process.exec(`ng serve --port=${process.env.DEV_SERVER_PORT}`);
child.stdout.on('data', data => console.log(data.toString()));

update package.json to provide this script running via npm

do not forget to set up DEV_SERVER_PORT environment variable (can be done via dotenv)

Here is also the complete description of this approach: How to change Angular CLI Development Server Port via .env.