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

@lucasbasquerotto thanks!

richardshergold avatar Nov 03 '17 11:11 richardshergold

Yet another webpack plugin to address this problem.

const EnvironmentSuffixPlugin = require('webpack-environment-suffix-plugin');
//...
new EnvironmentSuffixPlugin(
    ext: 'ts'
    suffix: process.env.NODE_ENV
);

AksimO avatar Nov 14 '17 22:11 AksimO

@gshigeto @lucasbasquerotto I got an error Module not found: Error: Can't resolve '@app/env', When set MY_ENV=prod

from https://github.com/gshigeto/ionic-environment-variables

ThunderBirdsX3 avatar Nov 28 '17 10:11 ThunderBirdsX3

@ThunderBirdsX3 Make sure you have this in your tsconfig.json:

"baseUrl": "./src",
"paths": {
  "@app/env": [
     "environments/environment"
  ]
}

Does the error happen with dev or just prod?

If your dev environment file is not environment.ts, but environment.dev.ts, then use

"baseUrl": "./src",
"@app/env": [
   "environments/environment.dev"
]

I don't know hou you are using the environment files, in my case I have the variables in json files and only 1 environment.ts, with the correct json file transferred, like I said above in the comments, and you can see it here.

lucasbasquerotto avatar Nov 28 '17 11:11 lucasbasquerotto

@lucasbasquerotto I made everything from https://github.com/gshigeto/ionic-environment-variables

And I got error when try MY_ENV=prod, But dev or test was fine.

ThunderBirdsX3 avatar Nov 28 '17 11:11 ThunderBirdsX3

@ThunderBirdsX3 I'm using the environment like in this repository

https://github.com/ajonp/angularfire2-env-issue

and not from the one you linked above, so I don't know exactly what you are having. Are you receiving the error in the build step or at runtime? I think it is in the build step, if so it is in the ngc step or webpack (or some other step)?

How are you environment files named? Make sure your webpack.config.ts generate the correct name (you may log it to see in your console). There is this line in the instructions from the link you showed:

var filePath = './src/environments/environment' + (env === 'prod' ? '' : '.' + env) + '.ts';

This means that if your env is prod the file is environment.ts and not environment.prod.ts. The error may be in the file names.

If this doesn't solve your problem I don't know what could be the problem. If you can create a demo repository on github and post the link I could try to reproduce in my machine.

lucasbasquerotto avatar Nov 28 '17 11:11 lucasbasquerotto

with latest ionic scripts, I can not get it to work with --prod. Checked out fresh from git and running npm run ionic:serve --prod gives the error, can not find module " @ app/env"

only thing i did is npm i -g ionic@latest

jhavinay avatar Nov 29 '17 10:11 jhavinay

Ionic only exposes two environments, dev and prod. so while using my own env variable to facilitate stage, test, etc, i had to add the alias to both dev and prod. everything works then. I can be more precise by finding out the ionic_env and adding my aliases to just that, but then.....

so new webpack.config.js is

var chalk = require("chalk");
var fs = require('fs');
var path = require('path');
var useDefaultConfig = require('@ionic/app-scripts/config/webpack.config.js');

var env = process.env.APP_ENV;
useDefaultConfig["dev"].resolve.alias = {
    "@myenv": path.resolve(environmentPath())
}
useDefaultConfig["prod"].resolve.alias = {
    "@myenv": path.resolve(environmentPath())
}

function environmentPath() {
    var filePath = './src/environments/environment.' + env + '.ts';
    if (!fs.existsSync(filePath)) {
        console.log(chalk.red('\n' + filePath + ' does not exist!'));
        return './src/environments/environment.dev.ts'
    } else {
        return filePath;
    }
}

module.exports = function () {
    return useDefaultConfig;
};

jhavinay avatar Nov 29 '17 14:11 jhavinay

@jhavinay that is correct. There is an open issue and update for that in my repo right now. I am sorry that I haven't gotten around to updating my repo, but that is what needs to happen with the latest ionic scripts!

gshigeto avatar Nov 29 '17 17:11 gshigeto

Still can't figure out how to get a QA env to get passed down. I am trying to use https://github.com/gshigeto/ionic-environment-variables

I have three environment.ts (prod, qa, dev).

I tried to update the actually app-scripts/webpack.config.js and added a new qa in that file (I'm thinking that is useless). Then in the local /config/webpack.config.js

can't figure out how to change the env. var env = process.env.IONIC_ENV;

I tried this. (using a new variable)

if(process.env.MY_ENV)
{
  env = process.env.MY_ENV;
}

then I would just run

MY_ENV=accept ionic cordova build android

It still building dev seems like. Even if its outputting the passed in var.

~/dev/new-mobile/gui $ MY_ENV=qa ionic cordova build android Running app-scripts build: --platform android --target cordova [11:39:22] build dev started ... [11:39:22] clean started ... [11:39:22] clean finished in 4 ms [11:39:22] copy started ... [11:39:22] deeplinks started ... [11:39:22] deeplinks finished in 272 ms [11:39:22] transpile started ...


I'm still missing something else.
Why is there no Ionic QA variable? Or is this an Angular thing.

newuser44 avatar Dec 18 '17 17:12 newuser44

in a nutshell, IONIC_ENV can only be dev or prod, and it depends on whether you are using --prod or --release options. You can define any number of MY_ENV, as explained by the posts above. And use the relevant files.

In your case, by using MY_ENV=accept ionic cordova build android your environment is "accept", not "qa". while IONIC_ENV is dev.

so if you followed the same principles it will be looking for environment.accept.ts file

in case you have something like MY_ENV=accept ionic cordova build android --prod MY_ENV is accept while IONIC_ENV Is prod.

I ended up using config like ionic:build:{{MY_ENV}}:{{IONIC_ENV}} in my package.json

jhavinay avatar Dec 19 '17 19:12 jhavinay

Yeah once I realized everything has to be dev or prod, I did a mix of both.

But in the webpack.config.js I added this. (updating what https://github.com/gshigeto/ionic-environment-variables had)

var env = process.env.IONIC_ENV;

if(process.env.MY_ENV) { console.log(process.env.MY_ENV) env = process.env.MY_ENV; } ...

useDefaultConfig.dev.resolve.alias = { "@app/env": path.resolve(environmentPath(env)) };

To build for dev

ionic cordova build browser --dev or MY_ENV=dev ionic cordova build browser (dev is default for ionic)

Acceptance/QA has to be MY_ENV=accept ionic cordova build browser

prod is the same as before ionic cordova build browser --prod

The only problem I had, is if I ran "ionic serve" to run things local using the proxy setting we have problems with cookies getting stored on the remote box and local host not having them. (We can't log in).

I fixed that by just adding in another environment called "no". environment.no.ts This just sets up the values as empty no no environment.

Change scripts to "ionic:serve": "MY_ENV=no ionic-app-scripts serve"

Then just run npm run ionic:serve

Does the same as ionic serve but still uses our proxy value for backend.

newuser44 avatar Dec 19 '17 19:12 newuser44

As a 15-year veteran Java/Spring developer/architect, the complexity of this boggles my mind. I don't understand how something so inherently simple as environment variables could be made so complex.

yerboogieman avatar Jan 05 '18 15:01 yerboogieman

i am struggling to do the same. for the time being im using the scripts object in package.json to configure for different environments like below.

"scripts": {
    "clean": "ionic-app-scripts clean",
    "build-dev": "cp ./env/config/environment-dev.ts ./src/config/environment.ts ; ionic-app-scripts build",
    "build-qa": "cp ./env/config/environment-qa.ts ./src/config/environment.ts && ionic-app-scripts build ",
    "build-prod": "cp ./env/config/environment-prod.ts ./src/config/environment.ts ;ionic-app-scripts build --prod",
}

Now i execute below for qa build. npm run build-qa

** I have all three env configurations ina folder called env in the project root. the cp command simply copies the necessary file into a config folder in src with the right name.

JayRaparla avatar Jan 12 '18 20:01 JayRaparla

@yerboogieman It is silly, but the complexity stems from lack of environment variables in the browser. Environment variables are the best thing ever. They're so good, webpack and similar tools do a sed-style replace of process.env.MY_VAR within code for the browser. But there's no standard way of having environmental config in the browser, so it's convoluted.

Anyway, for those following this issue, Ionic Angular v4 will support environmental config through the Angular CLI. Once your project is upgraded, the benefits of the Angular CLI come naturally. Therefore, I can finally put a milestone on this issue.

For Ionic Angular v2/v3, the tooling won't make use of the Angular CLI. It will continue using our own @ionic/app-scripts. I don't know if we'll revisit environmental config within app-scripts, but I do know that you can accomplish this today with the various methods listed in this issue and you can also use a custom webpack config entirely: https://github.com/ionic-team/ionic-app-scripts#overriding-config-files, which unlocks a bunch of features, but becomes much more difficult to maintain and configure.

imhoffd avatar Jan 17 '18 20:01 imhoffd

Well, fair enough @dwieeb and thank you very much for the detailed explanation!

yerboogieman avatar Jan 20 '18 06:01 yerboogieman

+1 (what's the correct way to vote on a feature request?)

zaarheed avatar Feb 17 '18 20:02 zaarheed

@zuperm4n Use the thumbs up emoji on the first post

iamjoyce avatar Feb 21 '18 03:02 iamjoyce

@dwieeb can we get an ETA for Ionic Angular 4 please? Weeks, months? Be nice to get a ballpark so we can figure out whether to implement one of the many workarounds or hold out.

GFoley83 avatar Feb 21 '18 19:02 GFoley83

+1 for this feature request

renweibo avatar Feb 22 '18 13:02 renweibo