How to set environment via `ng serve` in Angular 6

Cover Image for How to set environment via `ng serve` in Angular 6
Matheus Mello
Matheus Mello
published a few days ago. updated a few hours ago

🚀🔧 How to set environment via ng serve in Angular 6

So you've updated your Angular 5.2 app to Angular 6, followed all the instructions in the Angular update guide, and now you're trying to serve your app with a specific environment using the ng serve command. However, you encountered an error message saying "Unknown option: '--env'". Don't worry, I've got you covered! In this guide, I'll explain why this error occurs and provide you with easy solutions to set the environment in Angular 6. Let's dive in! 💪

Why the error occurs

In Angular 6, the option to set the environment via --env has been removed. Instead, Angular now uses the concept of configurations defined in the angular.json file. This change was introduced to improve the build and serve processes and make them more flexible.

Easy solution #1: Use configurations in angular.json

To set the environment in Angular 6, you need to define the configurations in your angular.json file. This file is automatically created when you generate a new Angular project using the Angular CLI. Here's how you can do it:

  1. Open the angular.json file in your project's root folder.

  2. Look for the "configurations" section and find the configuration you want to use (e.g., "local").

  3. Within the configuration object, you can specify the necessary properties such as "fileReplacements" or "assets" according to your environment.

Example of a configuration for the "local" environment:

"configurations": {
  "local": {
    "fileReplacements": [
      {
        "replace": "src/environments/environment.ts",
        "with": "src/environments/environment.local.ts"
      }
    ],
    "assets": [
      "src/favicon.ico",
      "src/assets"
    ]
  }
}
  1. Save the changes in the angular.json file.

  2. Now you can serve your app with the desired environment using the --configuration flag.

For example, to serve your app with the "local" configuration:

ng serve --configuration=local

Easy solution #2: Define custom configurations

Sometimes, you may want to define custom configurations based on your specific needs. Angular 6 allows you to do that as well. Here's how:

  1. Open the angular.json file in your project's root folder.

  2. Under the "projects" section, find the project you're working on (e.g., "my-app").

  3. Within the project object, you can add a "architect" section if it doesn't already exist.

  4. Inside the "architect" section, add a "configurations" section with the desired configuration name.

Example of adding a custom configuration called "dev":

"my-app": {
  "architect": {
    "configurations": {
      "dev": {
        "fileReplacements": [
          {
            "replace": "src/environments/environment.ts",
            "with": "src/environments/environment.dev.ts"
          }
        ],
        "assets": [
          "src/favicon.ico",
          "src/assets"
        ]
      }
    }
  }
}
  1. Save the changes in the angular.json file.

  2. Now you can serve your app with the custom configuration using the --configuration flag.

For example, to serve your app with the "dev" configuration:

ng serve --configuration=dev

That's it! 🎉 You should now be able to set the environment in Angular 6 using the new configuration system.

📣 Call-to-action: Share your success story!

I hope this guide helped you resolve the issue and set the environment in your Angular 6 app using the ng serve command. If you found this guide useful, consider sharing it with your friends and colleagues who might be facing the same problem. And don't forget to follow my blog for more exciting Angular tips and tricks! 😊


More Stories

Cover Image for How can I echo a newline in a batch file?

How can I echo a newline in a batch file?

updated a few hours ago
batch-filenewlinewindows

🔥 💻 🆒 Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Matheus Mello
Matheus Mello
Cover Image for How do I run Redis on Windows?

How do I run Redis on Windows?

updated a few hours ago
rediswindows

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! 🚀 Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Matheus Mello
Matheus Mello
Cover Image for Best way to strip punctuation from a string

Best way to strip punctuation from a string

updated a few hours ago
punctuationpythonstring

# The Art of Stripping Punctuation: Simplifying Your Strings 💥✂️ Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Matheus Mello
Matheus Mello
Cover Image for Purge or recreate a Ruby on Rails database

Purge or recreate a Ruby on Rails database

updated a few hours ago
rakeruby-on-railsruby-on-rails-3

# Purge or Recreate a Ruby on Rails Database: A Simple Guide 🚀 So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? 🤔 Well, my

Matheus Mello
Matheus Mello