Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

React Native 0.62.1 [iOS] - Empty config file in JS #444

Open
ahartzog opened this issue Apr 5, 2020 · 32 comments
Open

React Native 0.62.1 [iOS] - Empty config file in JS #444

ahartzog opened this issue Apr 5, 2020 · 32 comments

Comments

@ahartzog
Copy link

ahartzog commented Apr 5, 2020

Hey folks,

Just upgraded my project to react-native 0.62.1

In addition, I migrated react-native-config from 0.11.5 all the way to 1.0.0

I've updated my info.plist variables to match the new formatting, removed the old pre-processing scripts, added my build pre-actions copy and xcconfig setup. I believe my info.plist is actually pulling in variables properly, huzzah!

However, inside my actual JS, my Config
image

Is giving me a blank object.

I'm still loading in the envfiles the exact same way as before.

image

If other folks upgrading are experiencing this, or if anyone has direction for me, it would be hugely appreciate.

@luancurti
Copy link
Collaborator

Hi @ahartzog Do you have a reproducible demo?

@ahartzog
Copy link
Author

ahartzog commented Apr 8, 2020

@luancurti I do not, unfortunately other demands on my time mean I can't spin up a brand new 0.62 and re-create this

If this issue is common as people upgrade to 0.62, I'd like to see where we land. If it isn't, then I'll just have to find which esoteric hidden mis-configuration in my project is causing the issue.

Thank you for the response!

@makarkotlov
Copy link

makarkotlov commented Apr 10, 2020

RN 0.62.2
react-native-config 1.0.0

I managed to solve it by deleting this workaround #414 (comment) and deleting workaround in Podfile

installer.pods_project.build_configurations.each do |config|
     if config.name == 'Staging'
         config.build_settings['CONFIGURATION_BUILD_DIR'] = '$(BUILD_DIR)/Release$(EFFECTIVE_PLATFORM_NAME)'
     end
 end

Then I run react-native link && cd ios && pod install.
Then I cleaned project. Also I disabled Info.plist preprocessing in Build Settings -> Packaging -> Preprocess Info.plist File.
Also I have this in my android/app/build.gradle

project.ext.envConfigFiles = [
    releasestaging: ".env.staging",
    release: ".env.production",
    anycustombuildlowercase: ".env",
]

apply from: project(':react-native-config').projectDir.getPath() + "/dotenv.gradle"

@tr3v3r
Copy link

tr3v3r commented Apr 13, 2020

@ahartzog try to remove envfile from /tmp directory.
On mac go to Finder -> Go -> Go to Folder -> /tmp

The ruby script tries to find env file firstly from tmp/envfile. Then if it doesn't find anything searches in ENVFILE variable.

Hope this helps

@ahartzog
Copy link
Author

@makarkotlov good call on clearing out the react-native-config, I had forgotten to do that when updating from manual linkage.

Running the link on it isn't changing anything in the iOS config for me (and autolinking should work right in theory, according to @luancurti in another post)

I'm still not seeing my env variables in my JS - I've noticed that the pre-build script
image

Creates that envfile inside private/tmp - and then I still have no idea what this file is supposed to be doing or pointing to. Should I point it to my actual env file with an "absolute" url e.g.
image

Somebody mentioned that this file just works as a pointer to the actual file - I've also tried deleting it like the prior poster mentions but as far as I can tell this has no effect.

Still trying to figure this out here - would appreciate any additional ideas.

@ahartzog
Copy link
Author

I went ahead and tried @maxkomarychev 's new library, react-native-ultimate-config.

So far so good, iOS worked easily. Android fired up like a charm once I removed all the manual crap I had in place from RNC. JS config works, build configs work.

I would consider moving back to this when it's actually a part of react-native-community and supported, but until then I'll be with RNUC.

@Ajaykumar02
Copy link

I am facing issue in reading the config value it is undefined on reading Config.API_URL with the import Config from "react-native-config";
RN 0.61.5
react-native-config 1.0.0

Only issue is with Ios and works fine with Android platform

@tr3v3r
Copy link

tr3v3r commented Apr 20, 2020

I am facing issue in reading the config value it is undefined on reading Config.API_URL with the import Config from "react-native-config";
RN 0.61.5
react-native-config 1.0.0

Only issue is with Ios and works fine with Android platform

Could you please give us more info?
How many .env files do you have?
Do you use pre-action script?
Could you please check content of your tmp/envfile on your PC / Mac ?

@Ajaykumar02
Copy link

I am facing issue in reading the config value it is undefined on reading Config.API_URL with the import Config from "react-native-config";
RN 0.61.5
react-native-config 1.0.0
Only issue is with Ios and works fine with Android platform

Could you please give us more info?
How many .env files do you have?
Do you use pre-action script?
Could you please check content of your tmp/envfile on your PC / Mac ?

i had to restart my machine, it works for me.

@wbercx
Copy link

wbercx commented Apr 22, 2020

For me (RN0.62.2 + v1.0.0), it's not that the config is empty - it's returning the contents of my react-native-config.js, not my .env file...

import Cfg from 'react-native-config';
console.warn('CONFIG', Cfg);

prints:

'CONFIG', { dependencies: { 'react-native-flipper': { platforms: { ios: null } } } }

I suspect it's loading this.

EDIT:

// iOS:
import Cfg from 'react-native-config'; // Returns react-native-config.js contents.
import Cfg from 'react-native-config/index'; // Returns .env file contents. 

🤯

@tr3v3r
Copy link

tr3v3r commented Apr 22, 2020

@wbercx But the file with dependencies you've mentioned is called react-native.config.js. Not react-native-config.js.

@wbercx
Copy link

wbercx commented Apr 23, 2020

@wbercx But the file with dependencies you've mentioned is called react-native.config.js. Not react-native-config.js.

🤦 You're absolutely right. Thanks for pointing that out.

It's been a while since one character cost me this much time haha.

@gevgasparyan
Copy link

If you are using Pods just add (without react-native link react-native-config)

pod 'react-native-config', :path => '../node_modules/react-native-config/react-native-config.podspec'
in your Podfile

@Desteel
Copy link

Desteel commented May 22, 2020

I installed the package according to the instructions, and it worked... for a while. After switching branches, with code changes, etc., the config started returning an empty object.
I tried reinstalling modules and pods, clearing the cache, in the end I got the config data, only writing explicitly

echo ".env" > /tmp/envfile 

@RosenTomov
Copy link

RosenTomov commented May 28, 2020

Hello, I'm having the same problem with the latest version, but for Android on Windows.

OS - Windows 10 Pro 64-bit
Node - 12.16.3
RN - 0.62.2
react-native-config - v1.2.0

I've tried everything in the documentation and supposed fixes from random issues.
React-native-config v1.1.0 works, v1.2.0 returns an empty object.

Anyone else having this issue?

Edit:
Ok, so a colleague downloaded and sent me the 1.2.0 package, now it returns the env. variables, instead of an empty object. He too is with Win10 Pro, Node 12.16.3. 🤔

@MattyK14
Copy link

Unless I name one of my files to .env I'm also getting an empty Config on iOS.

react-native: 0.61.5
react-native-config:1.2.1

@MattyK14
Copy link

I was able to get it to work by changing the Pre-action Build script to:

echo ".env.stage" > /tmp/envfile

@bawahakim
Copy link

echo ".env.stage" > /tmp/envfile

@MattyK14 Thanks that worked for me!

@arabold
Copy link

arabold commented Jun 26, 2020

The README.md instructions seem to be misleading on this one. While at one point it mentions the tmp/envfile in step 6 of the iOS setup, the actual iOS Multi-Scheme section instructs us to use cp ${PROJECT_DIR}/../.env.staging .env as pre-action.

However, I could not get the pre-action work using the cp command. Switching the command to echo ".env.stage" > /tmp/envfile as suggested before works perfectly.

@skorp
Copy link

skorp commented Jul 18, 2020

IOS:
react-native-config@1.3.1
react-native@0.62.2

this in pre-action worked for me

echo ".env.staging" > .env

this not:

echo ".env.staging" > /tmp/envfile

update:
see comment below

@devzhen
Copy link

devzhen commented Jul 22, 2020

I have installed react-native-config according to docs and I have empty config.

Sure, echo ".env.staging" > /tmp/envfile - fix this issue.

but why we have such a case?

@skorp
Copy link

skorp commented Jul 27, 2020

so I have to correct something
it's very strange sometimes it works sometimes not.

for 9 days this worked:

echo ".env.staging" > .env

for 2 days this worked
cp ${PROJECT_DIR}/../.env.staging .env

and yesterday no one of the above commands worked:
also not this:
echo ".env.staging" > /tmp/envfile

I think the pre-actions are being executed too late,
because on the second run it works sometimes

at the moment i'm copying my stage env variables manually into .env

@agusvazquez
Copy link

Hello guys!

I am having this exact same issue with react-native 0.63.2 and react-native-config 1.3.2.

Is there a workarround for this?

Cannot make it work on iOS, Android works fine

Thanks!

@ronkot
Copy link

ronkot commented Aug 4, 2020

My problem was that I mistakenly added echo ".env.staging" > /tmp/envfile to the Run scripts. When changed to use Build scripts instead got this to work.

(Could not get the more clean cp ${PROJECT_DIR}/../.env.staging .env version to work, though.)

@agusvazquez
Copy link

@ronkot Thanks for your reply, I have already tried that without success :(

My project structure is
root/.env
root/.env.production
root/ios/etc
root/android/etc

Should I write

echo ".env.production" > /tmp/envfile

or

echo "../.env.production" > /tmp/envfile

?

@mrigo
Copy link

mrigo commented Aug 21, 2020

Works for me too.
echo ".env.production" > /tmp/envfile

But to change the info.plist information like the display name I've created two different targets, I couldn't read any entry of the config...

@agusvazquez
Copy link

agusvazquez commented Aug 21, 2020

I ended up using the following, since I only need them to be visible on the React Native (JS) side.

staging.json
production.json

scripts/set-environment.js

#!/bin/node
const fs = require('fs');
//Obtain the environment string passed to the node script
const environment = process.argv[2];
//read the content of the json file
const envFileContent = require(`../envs/${environment}.json`);
//copy the json inside the env.json file
fs.writeFileSync('env.json', JSON.stringify(envFileContent, undefined, 2));

Finally, I call staging or prod depending on the case

node scripts/set-environment.js staging
node scripts/set-environment.js production

@rares-lupascu
Copy link

same error here with the solution from the docs

@ylkhayat
Copy link

ylkhayat commented Sep 16, 2020

I was wondering if anyone found a potential fix.

Still having the issue of the empty config file after following the official documentation in step number 6 (iOS).

React Native v0.63.2
React Native Config v1.3.3

@markrickert
Copy link

I was having this issue today but i was getting values had changed in my .env file. The only way to get the proper env vars was to delete everything in /tmp (mac os) and recompile the app. Now it works.

@rickysullivan
Copy link

Make sure you remove /tmp/envfile if you've changed from the old echo method to the new cp one. I was pulling my hair out trying to figure out why and it turns out the Ruby file still checks for /tmp/envfile https://github.com/luggit/react-native-config/blob/master/ios/ReactNativeConfig/ReadDotEnv.rb#L15

@YDaskaloff
Copy link

YDaskaloff commented Aug 8, 2022

It happened to me as well.
I had followed this tutorial where you add pre-run script to your schema. In the tutorial the script looked like this:
cp "${PROJECT_DIR}/../.env.development" "${PROJECT_DIR}/../.env.development"

You can probably guess that the second .development was unnecessary.
I removed it and followed the same principle for the other schemas as well. It worked.

Of course you need to have an empty .env file along with your regular env files for this to work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests