Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch between two .env files - for prod and dev #38

Open
superKalo opened this issue Jul 8, 2019 · 2 comments
Open

Switch between two .env files - for prod and dev #38

superKalo opened this issue Jul 8, 2019 · 2 comments

Comments

@superKalo
Copy link

Hi. I've integrated this plugin on my Expo (React Native) project following these steps.

Can you please tell me if it is possible to configure the plugin to use the .env file for development and another file, .env.production, for production?

@Gregoirevda
Copy link

cp .env.{staging} .env

@aiham
Copy link

aiham commented Jul 10, 2019

@superKalo In my babel.config.js file I have this:

const fs = require('fs');
const path = require('path');

module.exports = function(api) {
  api.cache(true);

  const envName = process.env.MY_ENV_NAME || 'local';
  const dotEnvPath = path.resolve(__dirname, 'config', `${envName}.env`);
  if (!fs.existsSync(dotEnvPath)) {
    throw new Error(
      `Babel config couldn't find dot env file path: ${dotEnvPath}`,
    );
  }
  console.info(`Loading environment variables from: ${dotEnvPath}`);

  return {
    presets: ['babel-preset-expo'],
    plugins: [['inline-dotenv', { path: dotEnvPath }]],
  };
};

and in package.json I have:

  "scripts": {
    "start": "expo start",
    "start:prod": "MY_ENV_NAME=prod yarn start",
    "start:staging": "MY_ENV_NAME=staging yarn start"
  }

Sometimes the babel config gets cached so you'll need to add --clear to the start command.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants