react-native-config lib returns empty Config object

1.2k views Asked by At

I am having trouble using react-native-config in my React Native app. Here's my code:

app.js

import Config from 'react-native-config';
console.log(Config);

.env

SECRET_KEY="kndlknv94ii4rpp"

The console.log statement in app.js returns an empty object. What am I doing wrong?

API keys should return on Config.SECRET_KEY but Config object itself returns an empty object

4

There are 4 answers

0
Sadia Chaudhary On

I believe it's because of your env file format. As in the doc example, they are not using quotation marks. This might be a solution.

2
Nensi Kasundra On

Create a new file .env in the root of your React Native app:

API_URL=https://myapi.com
GOOGLE_MAPS_API_KEY=abcdefgh

Then access variables defined there from your app:

import Config from "react-native-config";

Config.API_URL; // 'https://myapi.com'
Config.GOOGLE_MAPS_API_KEY; // 'abcdefgh'

Documentation here.

1
ssj_100 On

Did you do the "Extra Step For Android" as mentioned in the docs?

The following needs to be added to android/app/build.gradle. Make sure to add it on the 2nd line.

// 2nd line, add a new apply:
apply from: project(':react-native-config').projectDir.getPath() + "/dotenv.gradle"
0
DrSiemer On

If you are using Expo you do not need any other libraries like react-native-dotenv or react-native-config. Just make sure your environment variables follow this format: EXPO_PUBLIC_[NAME]=VALUE. Then you will be able to access them using process.env.EXPO_PUBLIC_[NAME].