246

I am trying to update my Angular 5.2 app to Angular 6. I successfully followed instructions in the Angular update guide (including the update of angular-cli to v6), and now I am trying to serve the app via

ng serve --env=local

But this gives me error:

Unknown option: '--env'

I use multiple environments (dev/local/prod), and this is the way it was working in Angular 5.2. How can I set the environment now in Angular 6?

jowey
  • 7,581
  • 6
  • 27
  • 46
Martin Adámek
  • 16,771
  • 5
  • 45
  • 64

8 Answers8

452

You need to use the new configuration option (this works for ng build and ng serve as well)

ng serve --configuration=local

or

ng serve -c local

If you look at your angular.json file, you'll see that you have finer control over settings for each configuration (aot, optimizer, environment files,...)

"configurations": {
  "production": {
    "optimization": true,
    "outputHashing": "all",
    "sourceMap": false,
    "extractCss": true,
    "namedChunks": false,
    "aot": true,
    "extractLicenses": true,
    "vendorChunk": false,
    "buildOptimizer": true,
    "fileReplacements": [
      {
        "replace": "src/environments/environment.ts",
        "with": "src/environments/environment.prod.ts"
      }
    ]
  }
}

You can get more info here for managing environment specific configurations.

As pointed in the other response below, if you need to add a new 'environment', you need to add a new configuration to the build task and, depending on your needs, to the serve and test tasks as well.

Adding a new environment

Edit: To make it clear, file replacements must be specified in the build section. So if you want to use ng serve with a specific environment file (say dev2), you first need to modify the build section to add a new dev2 configuration

"build": {
   "configurations": {
        "dev2": {

          "fileReplacements": [
            {
              "replace": "src/environments/environment.ts",
              "with": "src/environments/environment.dev2.ts"
            }
            /* You can add all other options here, such as aot, optimization, ... */
          ],
          "serviceWorker": true
        },

Then modify your serve section to add a new configuration as well, pointing to the dev2 build configuration you just declared

"serve":
      "configurations": {
        "dev2": {
          "browserTarget": "projectName:build:dev2"
        }

Then you can use ng serve -c dev2, which will use the dev2 config file

David
  • 33,444
  • 11
  • 80
  • 118
  • do you know if it's possible to also add base-href in the configurations? or just in ng build --c staging --base-href = /yyy/ – Eduardo Tolino May 04 '18 at 18:14
  • @EduardoTolino: Yes you can, there is a `baseHref` option – David May 10 '18 at 06:03
  • Where could I specify the remote debugging port for the dev environment ? So as to debug with `VSCode`. In `Angular 6` of course. So that the `ng serve` command includes the remote debugger port when launching the `Chrome` browser. – Stephane May 17 '18 at 09:42
  • info link updated: https://github.com/angular/angular-cli/wiki/stories-application-environments – Mateo Tibaquira Jun 06 '18 at 02:37
  • 1
    ng serve -c local take time to compile the application comparing to "ng serve" – Vignesh Sep 03 '18 at 10:53
  • ng serve in angular-cli 6 don't work with file replacement https://github.com/angular/angular-cli/issues/11540#issuecomment-406030775 – Mr Jedi Sep 04 '18 at 08:12
  • Be sure that in your component, you import `import { environment } from './../environments/environment';` instead of `import { environment } from './../environments/environment.prod'` The "intelligent" code help imported the latter for me. – Brian Davis Sep 04 '18 at 14:54
  • The `"serve"` part saved my day, thank you. I didn't noticed that migrating environments only to the `"build"` section is not enough. – Yoraco Gonzales Sep 30 '18 at 12:59
  • Is anyone here aware how to enable debugging while working with multiple environments ? I am unable to debug while using QA Environment, check this out - https://stackoverflow.com/questions/56045274/typescript-debugger-statement-does-not-work-while-working-with-qa-environme/56045408#56045408 – curious_debugger May 08 '19 at 17:22
  • Thanks for this, I always wanted to have custom configurations on serve, but just now I had the time to do some research and found this! :) – Sampgun Sep 25 '20 at 14:40
63

This answer seems good.
however, it lead me towards an error as it resulted with
Configuration 'xyz' could not be found in project ...
error in build.
It is requierd not only to updated build configurations, but also serve ones.

So just to leave no confusions:

  1. --env is not supported in angular 6
  2. --env got changed into --configuration || -c (and is now more powerful)
  3. to manage various envs, in addition to adding new environment file, it is now required to do some changes in angular.json file:
    • add new configuration in the build { ... "build": "configurations": ... property
    • new build configuration may contain only fileReplacements part, (but more options are available)
    • add new configuration in the serve { ... "serve": "configurations": ... property
    • new serve configuration shall contain of browserTarget="your-project-name:build:staging"
iamrajshah
  • 963
  • 1
  • 11
  • 23
garfunkel61
  • 1,459
  • 12
  • 10
15

Angular no longer supports --env instead you have to use

ng serve -c dev

for development environment and,

ng serve -c prod 

for production.

NOTE: -c or --configuration

Dale K
  • 25,246
  • 15
  • 42
  • 71
Shilpa Soni
  • 306
  • 3
  • 7
12

You can try: ng serve --configuration=dev/prod

To build use: ng build --prod --configuration=dev

Hope you are using a different kind of environment.

Roy Scheffers
  • 3,832
  • 11
  • 31
  • 36
amku91
  • 1,010
  • 11
  • 21
  • ng serve --configuration=dev/prod command takes time comparing to ng serve why? – Vignesh Sep 03 '18 at 10:55
  • 2
    ng serve --configuration=prod takes more time then ng serve --configuration=prod because of file minimization and due to production ready code. – amku91 Sep 03 '18 at 11:49
  • ok if i use "ng serve --configuration=dev" command means its also taking more time – Vignesh Sep 04 '18 at 04:35
  • 1
    No, it shouldn't take more time. The prod environment has extra steps to minify, uglify and optimize the code for production. The other environments should take the normal time, unless you explicitly activate these extra steps. – Reginaldo Camargo Ribeiro Dec 11 '18 at 15:54
9

For Angular 2 - 5 refer the article Multiple Environment in angular

For Angular 6 use ng serve --configuration=dev

Note: Refer the same article for angular 6 as well. But wherever you find --env instead use --configuration. That's works well for angular 6.

4

You can use command ng serve -c dev for development environment ng serve -c prod for production environment

while building also same applies. You can use ng build -c dev for dev build

2

Use this command for Angular 6 to build

ng build --prod --configuration=dev
Sateesh
  • 1,327
  • 9
  • 12
2

This works on Angular 12

step 1:

"serve":
  "configurations": {
    "staging": {
      "browserTarget": "projectName:build:staging"
    }

Step 2: npx ng run myapp:serve --configuration=staging

Ashran Haider
  • 151
  • 1
  • 9