How to specify environment via `ng build` in Angular 6 app

24,555

Solution 1

One needs to use the configuration option

ng build --prod --configuration=uat

or

ng build --prod -c uat

More information here

Also for ng serve same option as answered here

Solution 2

I have tested in an Angular 6 Project.

ng build --prod --configuration=uat doesn't seem to work as it only picks uat configuration when you run this command and ignores --prod flag and doesn't apply any optimization such as aot, minification and upglification etc.

Running ng build --prod --configuration=uat has the effect as same as only running ng build --configuration=uat. In order to apply any other configuration options we need to add them explicitly in the uat build options in angular.json

"configurations": {
        "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
        },
        "uat": {
          "fileReplacements": [
            {
              "replace": "src/environments/environment.ts",
              "with": "src/environments/environment.test.ts"
            }
          ],
          "optimization": true,
          "outputHashing": "all",
          "sourceMap": false,
          "extractCss": true,
          "namedChunks": false,
          "aot": true,
          "extractLicenses": true,
          "vendorChunk": false,
          "buildOptimizer": true
        }
      }
    }

Solution 3

you can try to use:

ng build --configuration=uat

Solution 4

you can try to use

ng build ---prod

Solution 5

Prod: ng build --prod
Qa: ng build --configuration=qa

angular.json
"production": {
              "fileReplacements": [
                {
                  "replace": "src/environments/environment.ts",
                  "with": "src/environments/environment.prod.ts"
                }
              ]
},
"qa": {
              "fileReplacements": [
                {
                  "replace": "src/environments/environment.ts",
                  "with": "src/environments/environment.qa.ts"
                }
              ]
}

PROD:
export const environment = {
  production: true,
  api : 'https://example.com'
}

QA:
export const environment = {
    production: true,
    api : 'https://example-Qa.com'
}

dev environment
export const environment = {
    production:false,
    api : 'https://example-dev.com'
}
Share:
24,555

Related videos on Youtube

Nehal Damania
Author by

Nehal Damania

Updated on June 13, 2020

Comments

  • Nehal Damania
    Nehal Damania almost 4 years

    In Angular 5, we could generate build for different environments using

    ng build --prod --env=uat
    

    After migration to Angular 6 the above command throws error

    Unknown option: '--env'
    
  • NitinSingh
    NitinSingh almost 6 years
    OP wants to set the build for a particular environment, so someway of specifying that is required. the --prod is primary a "release" build which does bundling and similar stuff, but environment itself can have configurations to be put in
  • aemonge
    aemonge over 5 years
    Why in the bloody H would thery remove that information from ng build --help . Thanks for the anser @nehal
  • Glitch
    Glitch about 5 years
    angular7 its ng build --prod --aot
  • Pace
    Pace over 4 years
    In at least Angular 8 there is no reason to specify both --prod and --configuration. --configuration overrides --prod. Source
  • Devner
    Devner over 4 years
    ng build --prod --configuration=uat is exactly what I was looking for. Thanks a bunch!