Environment variables in Ionic 2

- Stefan Welsch

If you want to use environment variables in Ionic (2.2.0), you can try the following way.

/src/config/config.dev.ts and /src/config/config.prd.ts

export const ENV = {
//0=debug, 1=info, 2=error
LOG_LEVEL: 0,
...
}
package.json

"config": {
"ionic_webpack": "./src/config/webpack.config.js"
}
/src/config/webpack.config.js

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

module.exports = function () {
useDefaultConfig.resolve.alias = {
"@app/config": path.resolve('./src/config/config.' + process.env.IONIC_ENV + '.ts')
};

return useDefaultConfig;
}

How to use it?

...
import { ENV } from '@app/config'

@Component({
selector: 'page-contact',
templateUrl: 'contact.html'
})
export class Test {
private envVar = ENV.LOG_LEVEL;
}