How to change angular port from 4200 to any other

后端 未结 29 1601
心在旅途
心在旅途 2020-12-04 05:59

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:

{
   &q         


        
相关标签:
29条回答
  • 2020-12-04 06:28

    This will automatically open your window as well

    ng serve -o --port 4801
    
    0 讨论(0)
  • 2020-12-04 06:29

    You can edit default port number that is configured in serve.js file.

    Path will be project_direcory/node_modules/angular-cli/commands/serve.js.

    Search for this line -> var defaultPort = process.env.PORT || 4200;
    Replace with this line -> var defaultPort = process.env.PORT || 5000;

    0 讨论(0)
  • 2020-12-04 06:29

    To run and open in the browser automatically use the flag -open or just -o

    ng serve -o --port 4200
    

    Note: The port 4200 is arbitrary. It can be any port of your choice

    0 讨论(0)
  • 2020-12-04 06:30

    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

    0 讨论(0)
  • 2020-12-04 06:30

    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.

    0 讨论(0)
提交回复
热议问题