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?
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 .
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.
Use command like this for Anuglar 6 to build
ng build --prod --configuration=dev
Or using alias:
ng build --prod --c=dev
Angular 7+ (8/9/10) - via CLI created project:
ng build --prod --configuration production
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With