Angular – –ng build –env=prod is not working

angular

I am using angular4(4.4.6) and CLI 1.4.3.
I tried to make environment variables like in this article:
https://alligator.io/angular/environment-variables/

I ended up with 3 files:
environment.ts

export const environment = {
  production: false,
  restUrl: 'http://localhost:3000/',
  socketUrl: 'http://localhost:2000'
};

environment.prod.ts

export const environment = {
  production: true,
  restUrl: 'http://139.130.4.5:3000/',
  socketUrl: 'http://139.130.4.5:2000'
};

and environment.staging.ts

export const environment = {
  production: true,
  restUrl: 'http://139.130.4.5:3000/',
  socketUrl: 'http://139.130.4.5:2000'
};

I use them as follows:

import { environment } from '../../environments/environment';
 constructor() {
    this.serverUrl = environment.restUrl;
    console.log('the env is:' this.serverUrl');
  }

and in .angular-cli.json I have the following:

"environmentSource": "environments/environment.ts",
      "environments": {
        "dev": "environments/environment.ts",
        "prod": "environments/environment.prod.ts",
        "staging": "environments/environment.staging.ts"
      }

and from some reason, when I run ng build --env=prod it compiles and everything, but the final product uses the localHost(dev) enviorment variables.

whats even more strange is that when I use ng server --env=prod it works perfectly, with production variables.

why is this happening? how can I build with prod or staging environment variables?

Best Answer

Use command like this for Anuglar 6 to build

ng build --prod --configuration=dev

Or using alias:

ng build --prod --c=dev
Related Topic