brysgo / babel-plugin-inline-dotenv

Load your .env file and replace 'process.env.MY_VARIABLE' with the value you set.
124 stars 13 forks source link

Expo React Native Environment Setup #143

Closed rockstar00711 closed 2 years ago

rockstar00711 commented 2 years ago

Hello. I am gonna set some environment variables something like API key. I am using expo and babel.

I have followed expo documentation but no luck. https://docs.expo.dev/guides/environment-variables/#from--babelconfigjs

I would appreciate if anybody has advice about this. Here is my babel.config.js Thanks.

//babel.config.js
module.exports = function(api) {
  api.cache(true);
  return {
    presets: [‘babel-preset-expo’],
    plugins: [
      [
        “module-resolver”, {
          “extensions”: [
            “.js”,
            “.ts”,
            “.tsx”,
            “.css”
          ],
          “root”: [ “./src” ],
          “alias”: {
            “assets”: “./src/assets”,
            “type”: “./src/type”,
          }
        },
      ],
      [
        “inline-dotenv”, {
          path: ‘.env’
        }
      ]
    ]
  };
};

//.env
API_KEY=something

//const.js
export const THIRDPARTY = {
  api_key: process.env.API_KEY,
}

//home.js
useEffect(() => {
  console.log("env_api_key", THIRDPARTY.api_key);    ////////undefined///////
})
brysgo commented 2 years ago

Hey @rockstar0711 - stackoverflow might be a better place for this question