最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - environment variables in babel.config.js - Stack Overflow

programmeradmin7浏览0评论

We need to modify certain configuration/variables in our React Native app (built using Expo) depending on environment (local/dev/staging/production). I've looked at a number of libraries meant for this purpose but all seem to have a flaw that breaks for our use case:

  • dotenv (breaks because it tries to access 'fs' at runtime, when it's no longer available since it's not pure JS package and can't be bundled by Expo)
  • react-native-config (can't use it with Expo because it needs native code as part of the plugin)
  • react-native-dotenv (kinda works but caches config internally and ignores any .env changes until the file importing the variable is modified)

As a cleaner alternative that does not require third party plugins, I'm considering using babel's env option and just listing all of the environments as separate json objects within babel.config.js. I'm not seeing much documentation or examples on this, however. Do I just add env field at the same level as presets and plugins that contains production, development, etc. fields as in example below:

module.exports = (api) => {
    api.cache(true);
    return {
        presets: [...],
        env: {
            development: {
                CONFIG_VAR: 'foo'
            },
            production: {
                CONFIG_VAR: 'bar'
            }
        }
    }
}

Would that work? And how would I access this CONFIG_VAR later in the code?

We need to modify certain configuration/variables in our React Native app (built using Expo) depending on environment (local/dev/staging/production). I've looked at a number of libraries meant for this purpose but all seem to have a flaw that breaks for our use case:

  • dotenv (breaks because it tries to access 'fs' at runtime, when it's no longer available since it's not pure JS package and can't be bundled by Expo)
  • react-native-config (can't use it with Expo because it needs native code as part of the plugin)
  • react-native-dotenv (kinda works but caches config internally and ignores any .env changes until the file importing the variable is modified)

As a cleaner alternative that does not require third party plugins, I'm considering using babel's env option and just listing all of the environments as separate json objects within babel.config.js. I'm not seeing much documentation or examples on this, however. Do I just add env field at the same level as presets and plugins that contains production, development, etc. fields as in example below:

module.exports = (api) => {
    api.cache(true);
    return {
        presets: [...],
        env: {
            development: {
                CONFIG_VAR: 'foo'
            },
            production: {
                CONFIG_VAR: 'bar'
            }
        }
    }
}

Would that work? And how would I access this CONFIG_VAR later in the code?

Share Improve this question asked Jul 16, 2019 at 15:05 Alexander TsepkovAlexander Tsepkov 4,1864 gold badges38 silver badges65 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 16

I just ran into the same issues when trying to setup environment variables in my Expo project. I have used babel-plugin-inline-dotenv for this.

  1. Install the plugin

     npm install babel-plugin-inline-dotenv
    
  2. Include the plugin and the path to the .env file in your babel.config.js file

module.exports = function(api) {
  api.cache(true);
  return {
    presets: ['babel-preset-expo'],
    env: {
      production: {
        plugins: [["inline-dotenv",{
          path: '.env.production'
        }]]
      },
      development: {
        plugins: [["inline-dotenv",{
          path: '.env.development'
        }]]
      }
    }
  };
};

  1. In your .env.production or .env.development files add environment variables like this:

     API_KEY='<YOUR_API_KEY>'
    
  2. Later in your code you can access the above variable like this:

     process.env.API_KEY
    

To access your env variables within the babel.config.js file, use the dotenv package like this:

   require('dotenv').config({ path: './.env.development' })
   console.log('API_KEY: ' + process.env.API_KEY)

   module.exports = function() {
   // ...
   }
发布评论

评论列表(0)

  1. 暂无评论