react-native-dotenv: In production Release failed - env variable not importing
On Production build ios React Native 0.63.2 build failed. From @env variable not importing.
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 2
- Comments: 68 (23 by maintainers)
Commits related to this issue
- v2.3.0 (#51) * change default `allowUndefined` from `false` to `true` * progress towards #38 but not a fix — committed to goatandsheep/react-native-dotenv by goatandsheep 4 years ago
- Merge branch 'main' into bugfix/gh-38 — committed to goatandsheep/react-native-dotenv by goatandsheep 3 years ago
I found a quick workaround, it seems xcode doesn’t resolve the path from the project directory source but from
ios/, and it is looking for a.envin this directory.So just
cp .env ios/orcp .env.production ios/will allow you to build.Note that the error triggered by
react-native-dotenvwill block the building of themain.jsbundlefile. This issue can be related to https://github.com/facebook/react-native/issues/18472 for some people.I played around with the file names for a bit and here’s what I found;
For iOS release,
.envis treated as.env.production. For iOS debug,.env.developmentworks.Hi @goatandsheep , I am building to Android. So it turns out that the reason my
API_URLin .env was not recognized is that it is aHTTP& notHTTPShence couldn’t run on production because On android, as of API Level 28, clear text traffic is also blocked by default. I fixed the issue by setting android:usesCleartextTraffic in the app manifest file.@goatandsheep I have tested on 2.4.0 and it seems to have not solved the issue for me.
I’ve changed the default for
v2.3.0. For the rest of the issues in this ticket, I’ve placed a bounty for $15If I move
.envand.env.productionto ios folder, then the problem is solved.“react-native”: “0.63.2”, “react-native-dotenv”: “^2.4.0”
iOS - debug build is good but iOS - release build doesn’t work.
As @Slals said, “cp .env ios”, then it works.
Waiting for new build for “react-native-dotenv”
Thanks @goatandsheep for this good package 😃
Hi there, thank you for this amazing package 😃
I’m having this issue too, in development mode it works perfectly but it fails in release mode (i’m using xCode)…
I changed in my
babel.config.jsthe keyallowUndefinedtotrueand the build works but I think it only prevents throw the error because my code is not taking the values from the.envfile.I’m using: “react”: “16.13.1”, “react-native”: “0.63.2”, “react-native-dotenv”: “^2.2.0”,
@oleksandr-dziuban Dirty workaround to fix failed builds in my case was to set
allowUndefined: truein a babel config file.@goatandsheep Same for me, all production builds failed
SyntaxError: ../ios/src/core/env.js: "RAYGUN_API_KEY" is not defined in .envMigration is done correctly@goatandsheep Its my config. Its running development mode but in production build its failed.
@goatandsheep Sure. I did read both in trying to fix this issue.
I also lost a lot of time trying to fix this for iOS builds and ended up having to add
BABEL_ENV=productionto all of my build commands and in the xcode build script.I don’t mean to be rude but it’s not exactly helpful when I (and many others) have followed the wiki and migration guides and are still experiencing this issue - I’m hazarding a guess at that there is some misconfiguration somewhere (in this package or otherwise) that is incorrectly setting
BABEL_ENVto"undefined", which is the root cause for this issue.I’ve provided you with a location where we can start looking for a potential fix - even something as simple as a check
BABEL_ENV === "undefined"falling back to (valid)NODE_ENV, as per what your wiki says?I think the
"undefined"check is a simple and reasonable addition as who is going to genuinely use.env.undefined?We fixed this issue our our build by referencing this:
https://github.com/facebook/react-native/issues/29351#issuecomment-657929014
Hopefully helps someone else stumbles onto this thread. H/T to Matt Burnett on our team for finding it.
I’m having the same problem (react-native is 0.63.2), but nothing of the above worked for me. I can build in debug, but not in production.
I think I know what the issue is. I’ll address it this weekend. As you pointed out, the multi-env code is not fully working yet.
No. In Dev build it was worked as expected.
@alizahid doh! i’m on an old version that doesn’t have those niceties. I’ll bump in the next version of the app
@goatandsheep what’s the
react-native-clean-projectcommand you use to clear the cache?Edit: nvm, I just ran
react-native clean-projectand answered “no” to all of the questions and it worked without restart!@ivanichi thanks.
Yes the restarts have consistently worked but it’s pretty painful when switching between environments for test and prod builds
@gabrieljoelc yes, it should be clean. and you need a cup of coffee for a bit. so need a restart. if you can’t to pick up .env after restart. try for run :
export BABEL_ENV = .yourenvIt’s a caching issue. I’ll be doing a bit of maintenance tomorrow 😊
https://paypal.me/kemalahmedmac
of course with pleasure. I want to send a donation via paypal. please give me your paypal email, thank you.
I’m happy it works for you! Please leave a tip if possible https://issuehunt.io/r/goatandsheep/react-native-dotenv/issues/38
I wish cache cleaning were easier. Maybe this? https://www.npmjs.com/package/react-native-clean-project
now
react-native run-android --variant=releasesuccess to pick up env.xxx I experimented until felt like a mummy, thank you this library is very useful for my case i changed :process.env.BABEL_ENVtoprocess.env.ENVFILEyarn start --reset-cachecd android && ./gradlew cleanwipe data avd emulatorrestart the computerand build again, the problem has been resolved
react native 0.63.3 dotenv 2.5.3
@goatandsheep thank you for your suggestions and reply, I will try to explore, if successful i will come back, if on my case doesn’t work I’ll be happy to contact via the link above
@goatandsheep will there be a fix so we don’t need to copy the .env files to the ios folder?
thank you. it works
As noted the 2.5.1 release should help you @ivanichi
@jack828 no worries. fix is in and published as v2.5.1
+1 for @Slals workaround. Same setup as @hung-nb.
Hi there, thanks for supporting this package. For me it is working fine on development but I can confirm that it is not working on release mode on iOS - I didn’t test on Android. I’ve just created a react-native project using CRA. My “babel.config.js” file looks like this (I don’t know what a “babelrc” file is)
I’m using: react: 16.13.1 react-native: 0.63.2 react-native-dotenv: 2.4.0
node v10.20.1 Xcode 11.4.1
Looking forward for a fix =)
@goatandsheep there are no restrictions set by default, so, I think, for the sake for consistency it’s better to have
allowUndefined: trueIf this is your project https://github.com/2manoj1/breactnative/blob/master/babel.config.js , make sure to add the module to your list of babel plugins
https://github.com/goatandsheep/react-native-dotenv/wiki/Migration-Guide#2