Feature request: Ionic 2 (dev/prod) environment variables configuration
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
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.
Hi All, I have integrated this (https://github.com/gshigeto/ionic-environment-variables), It works very nice!
Thanks!
@wilfredonoyola I use this too, thanks!
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
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)
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 π
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.
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
Great! As the requestor of this feature, I am so happy it is finally resolved.
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 .
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!
This is great news that this has arrived! However:
- 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.
- Is there an example/starter project where this is being used correctly?
- How does one specify the environment (prod/dev/etc) to the CLI?
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.
@dwieeb Thank you for your response.
- No matter whether --prod or --dev is used when running ionic serve/build the process.env.IONIC_ENV always specifies "dev" for me.
- 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!
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 π
Thank you @dwieeb, that clarifies things quite a lot.
Should this issue be closed? Considering the feature is not working correctly?
To clarify:
- --prod or --dev does not load the relevant .env.prod/.env.dev file during pack. It always loads the .env file.
- Custom Environment variables specified in the .env file do not get loaded into process.env
- IONIC_ENV variable is always dev no matter what cli commands are used.
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.
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