question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Multiple environment.ts are confusing

See original GitHub issue

The current model for environment variables is to have 3 places for files:

  1. /src/environment.ts This is here so when we type in VS Code or our favorite editor we get intellisense. it is not used for a build
  2. /config/environment.dev.ts and /config/environment.prod.ts This is where we put our env settings for the different environments. It is copied to the dist/ folder.
  3. /dist/environment.js This is the one that gets run, we do not edit this one.

Follow this scenario … we need an env variable to point to a web url for our APIs. So we create a

export const environment {
  url: '/api/foo`
};

and we put it in /src/environment.ts. We then import it with import { environment } from './environment. VS Code is happy (as is any tsc compiler command).

Now we run ng serve and browse to the app, and we see an error in the console about how it doesnt know what url is. Oops, we didnt put this in the config/environment.dev.ts (nor prod) files. OK, so we go do that and now we can run and serve smoothly.

Now … we add a new env for showDebugMessages: true. We set it to true for dev and false for prod. We recall that we need to do this in the /config/environment.*.ts files so we feel we are good. But we forgot we also need to put the same setting in the /src/environment.ts. It is not used … but we need something there. Do we out false or true for this new setting? It doesn’t matter because we are not using it.

export const environment {
  url: '/api/foo`,
  showDebugMessages: true
};

My point here is that it can be confusing on how to do this and not make a mistake.

Are there any thoughts on how to prescribe this? Or how to make it easier? Have you considered an interface?

My Env:

$ ng -v
angular-cli: 1.0.0-beta.5
node: 4.2.3
os: darwin x64

cc @spboyer

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:27
  • Comments:15 (5 by maintainers)

github_iconTop GitHub Comments

11reactions
serhiisolcommented, Jun 6, 2016

Also it would be great if we could make parent configuration, which config/environment.*.ts might extend, for instance:

_config/environment.base.ts_

export var environment = {
    API_BASE_URL: "http://example.com/api"
}

_config/environment.dev.ts_

import {extend} from 'lodash';
import {base} from './environment.base';

export var environment = extend(base, {
    dev: true
});

currently EmberCli base project reads and compiles only specific file based on version (prod or dev)

_angular-cli/lib/broccoli/environment.js_

  // Load the content of the environment file.
  const filePath = path.join(project.root, `config/environment.${env}.ts`);
  const source = fs.readFileSync(filePath, 'utf-8');
  const result = ts.transpile(source, {
    target: ts.ScriptTarget.ES5,
    module: ts.ModuleKind.CommonJs
  });

So what I’m saying is that you can’t import something and extend it later

5reactions
spboyercommented, May 27, 2016

Posted on how to use and the drawbacks here: https://tattoocoder.com/angular-cli-using-the-environment-option/

interface, at least in the current folder structure is not a good workflow. The interface would either need to be at the base root of the project or in a place where referencing it doesn’t make sense.

Read more comments on GitHub >

github_iconTop Results From Across the Web

The best way to use Angular's environment files - Sean G. Wright
Environment files in Angular, when using the CLI, are a great way to add per-environment configuration to your app. You can have your...
Read more >
Multi-environment setup for your Angular app
The Angular CLI bootstraps a new project with two files within the folder environments : environments.ts and environments.prod.ts.
Read more >
Toggle between multiple .env files like .env.development with ...
I think we've all been confused by the dotenv's wording. They say We strongly recommend against having a "main" .env file and an...
Read more >
Using shared settings between multiple environments in Angular
Great, let's add some configuration in our new env file... // environment.staging.ts export const environment = { production ...
Read more >
How to use System Environment variables (process.env) in ...
If we have multiple dev environments. ... In this case, we need to change LOG_LEVEL in our environment.ts file and rebuild. We don't...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found