Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 809
Yousuf Avatar asked Mar 14 '17 19:03

Yousuf


People also ask

How do I change my host in NG serve?

ng serve has built-in option flags to update host and port: --port : port to listen on; defaults to 4200. --host : host to listen on; defaults to localhost.

What command would you use to build and serve your app automatically opening a new browser window?

In your browser, open http://localhost:4200/ to see the new application run. When you use the ng serve command to build an application and serve it locally, the server automatically rebuilds the application and reloads the page when you change any of the source files.

How does angular know which environment?

angular-cli knows what environment to use, because you are passing the --environment flag. If no flag is passed, then dev environment is used.

How angular manage multiple environments CLI?

Adding multiple environments:Open your angular project in a preferred IDE. Navigate to src>environments, there you can see two files environment. ts and environment. prod.


2 Answers

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.

like image 107
NValchev Avatar answered Oct 04 '22 21:10

NValchev


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"
   }
 }
like image 43
Kalani Bright Avatar answered Oct 04 '22 19:10

Kalani Bright