ionic-cli icon indicating copy to clipboard operation
ionic-cli copied to clipboard

Feature request: Ionic 2 (dev/prod) environment variables configuration

Open jgw96 opened this issue 10 years ago β€’ 121 comments

From @amreladawy on July 24, 2016 6:58

Short description of the problem:

It is a feature request. Just like Angular2, I hope that Ionic can provide 2 files (environment.dev.ts and environment.prod.ts) that would contain variables with different values corresponding to production and development environment. During the build, the appropriate file to be copied and bundled within the app artifact.

Which Ionic Version? 2.x

https://forum.ionicframework.com/t/ionic-2-environment-variables-config-setup/58147/1

https://stackoverflow.com/questions/36004810/how-to-config-different-development-environment-in-angular-2-app

Copied from original issue: driftyco/ionic#7413

jgw96 avatar Jul 25 '16 20:07 jgw96

For those looking for a solution that supports ngc, karma & protractor tests, multiple configurations e.g test, uat etc. and works exactly like Angular CLI's environment setup import { environment } from '@env/environment';, I've posted my setup here: https://github.com/ionic-team/ionic-app-scripts/issues/762#issuecomment-367862651

Works by extending webpack's configuration so should be pretty easy to refactor out later whenever Ionic 4 drops with native Angular CLI support.

GFoley83 avatar Feb 25 '18 18:02 GFoley83

Hi All, I have integrated this (https://github.com/gshigeto/ionic-environment-variables), It works very nice!

Thanks!

wilfredonoyola avatar Feb 27 '18 16:02 wilfredonoyola

@wilfredonoyola I use this too, thanks!

tivonchen avatar Mar 16 '18 12:03 tivonchen

All, with Ionic 4 release approaching, I know that ENV vars will be handled. If you are using Ionic 3.9.2, here is what I have been using for months to handle ENV vars. It's been pieced together from this and a few other threads.

https://gist.github.com/tabirkeland/a17c67b2f1ea3331d94db34ed7191c34

tabirkeland avatar Mar 16 '18 16:03 tabirkeland

Changing the milestone to Ionic Angular 4. CLI 4.0.0 will likely be released before the upcoming Framework release.

CLI 4.0.0 Milestone: https://github.com/ionic-team/ionic-cli/milestone/36 Ionic Angular 4 Milestone for CLI: https://github.com/ionic-team/ionic-cli/milestone/38

See this issue to learn how to test Ionic Angular 4 in the alpha CLI today: https://github.com/ionic-team/ionic-cli/issues/3019 (warning: it's likely a bit early yet)

imhoffd avatar Mar 20 '18 19:03 imhoffd

I'm really not sure why this has gotten so complicated. As an example, in Ember.js (ember-cli), there is a config/environment.js file that imports the system environment variables process.env (including custom .env environment variables imported via dotenv) at build-time and allows you to configure what variables should be exposed to your app bundle.

Basically:

// config/environment.ts
module.exports = function(environment) {
  let ENV = {
     foo: environment == 'prod' ? process.env.FOO : 'baz';
  }
  return ENV;
}

This gives you the flexibility of making use of existing system environment variables as being able to easily declare extra project-specific env variables via the .env file

In other solutions I've seen above env variables are being declared as JS modules in the source code, which isn't a great idea, and not particularly flexible.

Maybe I'm missing something with the added complexity of building via cordova as well as the web, but this seems to me to be already solved elsewhere.

EDIT: I see that this is an upcoming feature of Ionic 4 so I'm looking forward to seeing it implemented as part of core πŸ‘

timmyomahony avatar Jul 04 '18 10:07 timmyomahony

Recently tried the solution proposed by @fiznool but failed. When I used the DefinePlugin inside the webpack config in my node_modules everything worked fine. But as soon as I extended the config the build broke with webpack service a 404 for all compiled resources.

This error also persisted when I simply copied the entire webpack config out of the node_modules folder.

We wanted to use the DefinPlugin to enable feature flags from the console when running ionic serve but sadly for now we are stuck with config files.

HoverBaum avatar Jul 19 '18 07:07 HoverBaum

Hi all! πŸ‘‹ As many of you may have seen, we announced Ionic 4 beta on our blog! πŸŽ‰

I'm closing this issue as it has been addressed with Ionic 4. If I'm mistaken, please let me know.

Here is some documentation on the Angular CLI's concept of application environments: https://github.com/angular/angular-cli/blob/master/docs/documentation/stories/application-environments.md

imhoffd avatar Jul 30 '18 19:07 imhoffd

Great! As the requestor of this feature, I am so happy it is finally resolved.

amreladawy avatar Jul 31 '18 03:07 amreladawy

It is fixed if you upgrade aka migrate

On Mon, Jul 30, 2018, 10:22 PM Amr ElAdawy [email protected] wrote:

Great! As the requestor of this feature, I am so happy it is finally resolved.

β€” You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/ionic-team/ionic-cli/issues/1205#issuecomment-409081756, or mute the thread https://github.com/notifications/unsubscribe-auth/AAP7O-_V4Or7xnbRBPUTEohiuPSAlhooks5uL815gaJpZM4JUhnW .

joefeser avatar Jul 31 '18 05:07 joefeser

For all those still subscribed to this issue, @nphyatt has put in support for this in @ionic/app-scripts to support environments for Ionic 3!

It runs a Webpack plugin which replaces usages of process.env.VAR with string literals, using actual environment variables during the build to do so. See https://github.com/ionic-team/ionic-app-scripts#environments for docs and https://github.com/ionic-team/ionic-app-scripts/pull/1471 for the PR. πŸŽ‰

Let us know what you think!

imhoffd avatar Aug 24 '18 20:08 imhoffd

This is great news that this has arrived! However:

  1. The docs link provided on the app-scripts readme.md is extremely ambiguous and not very helpful whatsoever to someone wanting to implement in their existing/new projects.
  2. Is there an example/starter project where this is being used correctly?
  3. How does one specify the environment (prod/dev/etc) to the CLI?

granttink avatar Sep 05 '18 13:09 granttink

Could you elaborate on how the documentation is ambiguous?

I do think the documentation assumes some knowledge about: environment variables, NodeJS's process.env, dev builds vs prod builds. But I also think this is fine. I suspect people who understand the concept of environments have likely come across the aforementioned concepts as well, but maybe I'm mistaken. In any case, I will look at PRs to app-scripts for improvements. πŸ™‚

There's no example/starter, but if someone makes one I'd be happy to link to it.

The environment is either dev (default) or prod via --prod.

imhoffd avatar Sep 05 '18 15:09 imhoffd

@dwieeb Thank you for your response.

  1. No matter whether --prod or --dev is used when running ionic serve/build the process.env.IONIC_ENV always specifies "dev" for me.
  2. The doc mentions ".env.dev" and ".env.prod" files in root of application. a. What is the full filename? anything.env.dev.ts? or did you mean env.dev.ts? b. What is the root of the application? ./src (where index.html resides) or ./ (where package.json resides)? c. is there an example what should the contents of these files should contain in order to alter environment variables?

Thank you in advance!

granttink avatar Sep 05 '18 16:09 granttink

Thanks @granttink! Good feedback.

IONIC_ENV not being set is a bug, probably something that was overlooked. I believe both it and NODE_ENV should be set to prod/production respectively if --prod is used.

Yeah, the files are actually optional, which I think should also be stated. Also, the documentation assumes some knowledge about the format of .env files. Also, we can make it clear that this is the webpack plugin we're using.

I added an issue to revise the documentation: https://github.com/ionic-team/ionic-app-scripts/issues/1474. Thank you πŸ‘

imhoffd avatar Sep 05 '18 18:09 imhoffd

Thank you @dwieeb, that clarifies things quite a lot.

granttink avatar Sep 05 '18 23:09 granttink

Should this issue be closed? Considering the feature is not working correctly?

granttink avatar Sep 12 '18 09:09 granttink

To clarify:

  1. --prod or --dev does not load the relevant .env.prod/.env.dev file during pack. It always loads the .env file.
  2. Custom Environment variables specified in the .env file do not get loaded into process.env
  3. IONIC_ENV variable is always dev no matter what cli commands are used.

granttink avatar Sep 12 '18 09:09 granttink

This issue will remain closed as it is now an available feature. There are minor issues with it, which should be tracked separately. I would prefer new issues for them. I will update this issue when they are fixed.

imhoffd avatar Sep 12 '18 15:09 imhoffd

Any update or work around on this issue? I’m trying to build my app with the β€”prod flag now and it’s still not switching out the variables properly

kyleabens avatar Oct 04 '18 11:10 kyleabens