React-native-config: Does this work with Expo?

Created on 26 Apr 2018  ·  6Comments  ·  Source: luggit/react-native-config

I'm stil using react-native-scripts and have not ejected, so I can't add custom gradle scripts. Should react-native-config still work, and do I need to set up something so that it works with the Expo app? I'm currently just getting an empty object for Config.

Most helpful comment

Same result here. Since it requires react-native link react-native-config I don't think it'll ever work with Expo unless we eject from Expo 🤷‍♂️

For reference I got my .env to work with the following:

$ npm install --save-dev babel-plugin-inline-dotenv
  • Added inline-dotenv to .babelrc:
{
  "plugins": ["inline-dotenv"]
}
  • Added a .env file:
HELLO_WORLD="Hello, world!"
  • Finally added the environment variable:
<Text style={styles.getStartedText}>{process.env.HELLO_WORLD}</Text>

Only real caveat is the React Native packager needs to be restarted between .env changes, either the hot-reloading isn't watching .env or babel doesn't run between hot-loading. Either way I'm happy with that as I'll be using .env to override config options in my app 🎉

All 6 comments

@Yogu I get the same thing. It doesn't seem to be working.

Same result here. Since it requires react-native link react-native-config I don't think it'll ever work with Expo unless we eject from Expo 🤷‍♂️

For reference I got my .env to work with the following:

$ npm install --save-dev babel-plugin-inline-dotenv
  • Added inline-dotenv to .babelrc:
{
  "plugins": ["inline-dotenv"]
}
  • Added a .env file:
HELLO_WORLD="Hello, world!"
  • Finally added the environment variable:
<Text style={styles.getStartedText}>{process.env.HELLO_WORLD}</Text>

Only real caveat is the React Native packager needs to be restarted between .env changes, either the hot-reloading isn't watching .env or babel doesn't run between hot-loading. Either way I'm happy with that as I'll be using .env to override config options in my app 🎉

I've spent a couple of hours trying to get @jdrydn's solution above to work, and to no avail. Seems everyone else has managed to though, so no idea why it's not working for me.

  • Starting the expo local server with exp start as needed for notification setup
  • Have installed both babel-plugin-inline-dotenv and dotenv (and tried without dotenv installed too), both options haven't worked
  • Have deleted node_modules and package_lock.json
  • Have installed first with yarn, then removed and installed with npm
  • Have tried adding inline-dotenv to .babelrc inside env.development as well as at the root
  • Restarted the server and closed and re-opened Expo app at every turn

Current .babelrc:

{
  "presets": ["babel-preset-expo"],
  "env": {
    "development": {
      "plugins": ["transform-react-jsx-source","inline-dotenv"]
    }
  },
  "plugins": ["inline-dotenv"]
}

Are there any steps I'm missing that aren't listed above? I'm at the end of myself here, I just want to use environment variables I can't believe I've spent half a day on this and it still doesn't work 😞

EDIT: Wow ok, so outputting process.env will just have { "NODE_ENV": "development" } but outputting process.env.HELLO_WORLD works (though it didn't when I first tried out my setup)

Make sure to output the specific ENV var, not the whole process.env folks.

I ejected from Expo. It works on the Java side, but the Javascript Config object remains empty

Fixed it by adding resValue "string", "build_config_package", "host.exp.exponent" per the documentation.

inline-dotenv plugin is working for me as described above. However every time I change the .env file I have to restart server and clean the cache by passing -c flag (otherwise the configuration won't change):

yarn start -c
Was this page helpful?
0 / 5 - 0 ratings

Related issues

francisrod01 picture francisrod01  ·  3Comments

ORESoftware picture ORESoftware  ·  3Comments

NachtRitter picture NachtRitter  ·  4Comments

Husnain-Asharf picture Husnain-Asharf  ·  3Comments

arcollector picture arcollector  ·  4Comments