Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

--ng build --env=prod is not working

Tags:

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?

like image 724
Efim Rozovsky Avatar asked Jan 08 '18 15:01

Efim Rozovsky


People also ask

Is ng build prod default?

ng build now uses the production configuration by default. You no longer need to pass the --prod option to the command. The --prod flag is deprecated, and we must now use --configuration production .

What does ng build -- prod does?

The ng build command is intentionally for building the apps and deploying the build artifacts. The command does not generate an output folder. The output folder is – dist/. The ng serve builds artifacts from memory instead for a faster development experience.


2 Answers

Use command like this for Anuglar 6 to build

ng build --prod --configuration=dev 

Or using alias:

ng build --prod --c=dev 
like image 136
Sateesh Avatar answered Sep 30 '22 09:09

Sateesh


Angular 7+ (8/9/10) - via CLI created project:

ng build --prod --configuration production

like image 26
Lonely Avatar answered Sep 30 '22 10:09

Lonely