angularangular-cli

angular-cli server - how to specify default port


Using angular-cli with the ng serve command, how can I specify a default port so I do not need to manually pass the --port flag every time?

I'd like to change from the default port 4200.


Solution

  • Update for @angular/cli@9.x: and over

    In angular.json you can specify a port per "project"

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

    All options available:

    https://angular.io/guide/workspace-config#project-tool-configuration-options

    Alternatively, you may specify the port each time when running ng serve like this:

    ng serve --port 1337

    With this approach you may wish to put this into a script in your package.json to make it easier to run each time / share the config with others on your team:

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

    Legacy:

    Update for @angular/cli final:

    Inside angular-cli.json you can specify the port in the defaults:

    "defaults": {
      "serve": {
        "port": 1337
      }
    }
    

    Legacy-er:

    Tested in angular-cli@1.0.0-beta.22-1

    The server in angular-cli comes from the ember-cli project. To configure the server, create an .ember-cli file in the project root. Add your JSON config in there:

    {
       "port": 1337
    }
    

    Restart the server and it will serve on that port.

    There are more options specified here: http://ember-cli.com/#runtime-configuration

    {
      "skipGit" : true,
      "port" : 999,
      "host" : "0.1.0.1",
      "liveReload" : true,
      "environment" : "mock-development",
      "checkForUpdates" : false
    }