Environment variables in Ionic 2

31.03.2017 Stefan Welsch
Mobile Tech ionic mad handson goodtoknow

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

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

export const ENV = {
    //0=debug, 1=info, 2=error
    LOG_LEVEL: 0,

"config": {
    "ionic_webpack": "./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 do you use it?

import { ENV } from '@app/config'

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

This text was automatically translated with our golang markdown translator.

Stefan Welsch - pioneer, stuntman, mentor. As the founder of b-nova, Stefan is always looking for new and promising fields of development. He is a pragmatist through and through and therefore prefers to write articles that are as close as possible to real-world scenarios.