1

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 Answers 4

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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"

1 Comment

OMG. Thanks!! This step should be more visible in the docs, not under the "optional linking setup" instructions...
1

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].

Comments

-3

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.

2 Comments

Does not address the issue that OP is having and simply repeats what OP mentioned.
What's the issue actually you are facing @ssj_100?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.