Search code examples
angulardeploymentbuildenvironmentserve

Angular set build command line parameter in config (--host / --disable-host-check for --configuration=production)


When I start my Angular project on my server I always need to add --disable-host-check and --host=0.0.0.0 for it to work. Since I don't need to do that in my local dev environment I would like to configure these parameters in my angular.json build configuration so that I can simply execute

ng serve --prod 

or

ng serve --configuration=staging 

on my server and have the host and disable-host-check set automatically in the corresponding build settings. Is that possible? Currently my config looks like this:

"configurations": {
        "develop": { 
          "fileReplacements": [
            {
              "replace": "src/environments/environment.ts",
              "with": "src/environments/environment.dev.ts"
            }
          ]
        },
        "staging": {
           "fileReplacements": [
            {
              "replace": "src/environments/environment.ts",
              "with": "src/environments/environment.staging.ts"
            }
          ]
         }
        "production": { 
          "fileReplacements": [
            {
              "replace": "src/environments/environment.ts",
              "with": "src/environments/environment.prod.ts"
            }
          ],
          "optimization": true,
          "outputHashing": "all",
          "sourceMap": false,
          "extractCss": true,
          "namedChunks": false,
          "aot": true,
          "extractLicenses": true,
          "vendorChunk": false,
          "buildOptimizer": true,
          "budgets": [
            {
              "type": "initial",
              "maximumWarning": "2mb",
              "maximumError": "5mb"
            }
          ]
        }

Solution

  • Instead of messing with the angular.json file why don't you just add a script for the command in package.json? For example:

      "scripts": {
        "start": "ng serve --prod --disable-host-check --host=0.0.0.0",
      }
    

    Then to call it it would just be npm run start.