Yousuf
Yousuf

Reputation: 3275

angular cli change default environment when running ng serve

When I run ng serve it uses default environment as dev. Is there a way to update angular cli to use a different environment file?

Update

Sorry, Should have been more clear in my question. I know we can use --env switch to specify the environment file to use, but was wondering if there is a way to change the default environment file selection when no env is specified. Currently we have environment.local.ts file, I am trying to update angular cli configuration to use environment.local.ts when no environment is specified.

Upvotes: 9

Views: 19248

Answers (5)

Nambi N Rajan
Nambi N Rajan

Reputation: 509

"options": {
            "browserTarget": "rcyc-admin:build:uat"
          },
For angular version 7 , you just need to set default serve->options->browserTarget to env file name like in my case its "browserTarget": "cat-admin:build:uat". Or you can add new inside serve->configurations with your environment files like this

"production": {
  "browserTarget": "rcyc-admin:build:production"
},
"uat": {              
   "browserTarget": "rcyc-admin:build:uat"
},

and call using

 ng serve -c uat

in angular-cli.json

Not recommended This is a simple server for use in testing or debugging Angular applications locally

Upvotes: 0

Kalani Bright
Kalani Bright

Reputation: 129

For Angular 6+ follow these instructions:

First you will define your environments in your angular.json, create those files, and then you will run via "ng serve --configuration dev"

This document covers the details of the above:

https://theinfogrid.com/tech/developers/angular/environment-variables-angular/

Additionally you will need to define the same in the "serve" section of the JSON...i.e:

"serve": {
 [...]
 "configurations": {
   "production": {
     "browserTarget": "myApp:build:production"
   },
   "debug": {
     "browserTarget": "myApp:build:debug"
   }
 }

Upvotes: 3

Shobhit Sharma
Shobhit Sharma

Reputation: 493

It may be issue with the @angular/cli version, please remove/uninstall the current version of your @angular/cli by using the following command.

"npm remove @angular/cli" --save-dev

and then install the latest version like :

"npm install @angular/[email protected]" --save-dev

I hope default environment issue will be solve.

Upvotes: 0

NValchev
NValchev

Reputation: 3005

I had the same issue as you, and couldn't find a way to override the default environment variable used when no env arg is passed, but found a workaround that worked for me:

Simply change the file path that is standing for the dev environment in the angular cli configuration file (angular-cli.json):

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

dev is the default environment variable used by Angular CLI if no args are passed, so the local file would be the used one.

Upvotes: 5

rhyneav
rhyneav

Reputation: 123

Try using the --environment option through the CLI:

ng serve --environment prod will use the environment.prod.ts file.

You can also configure your environments in .angular-cli.json file.

Hope this helps!

Upvotes: -1

Related Questions