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

Most upvoted comments

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 .env in this directory.

So just cp .env ios/ or cp .env.production ios/ will allow you to build.

Note that the error triggered by react-native-dotenv will block the building of the main.jsbundle file. 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, .env is treated as .env.production. For iOS debug, .env.development works.

Hi @brianwachira , I’m happy to help but what is the issue? What platform are you building to? Have you created your release / dev profiles on xcode/AS? Is this part of this ticket? This one has been closed for some time and probably should have been locked

Hi @goatandsheep , I am building to Android. So it turns out that the reason my API_URL in .env was not recognized is that it is a HTTP & not HTTPS hence 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 $15

If I move .env and .env.production to 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.js the key allowUndefined to true and the build works but I think it only prevents throw the error because my code is not taking the values from the .env file.

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: true in a babel config file.

module.exports = {
  presets: ['module:metro-react-native-babel-preset'],
  plugins: [
    [
      'module:react-native-dotenv',
      {
        moduleName: '@env',
        path: '.env',
        blacklist: null,
        whitelist: null,
        safe: false,
        allowUndefined: true,
      },
    ],
  ],
};

@goatandsheep Same for me, all production builds failed SyntaxError: ../ios/src/core/env.js: "RAYGUN_API_KEY" is not defined in .env Migration is done correctly

module.exports = {
  presets: [
    'module:metro-react-native-babel-preset'
  ],
  env: {
    production: {},
  },
  plugins: [
    [
      '@babel/plugin-proposal-decorators',
      {
        legacy: true,
      },
    ],
    ['@babel/plugin-proposal-optional-catch-binding'],
    ["module:react-native-dotenv", {
      "moduleName": "@env",
      "path": ".env",
      "blacklist": null,
      "whitelist": null,
      "safe": false,
      "allowUndefined": false
    }]
  ],

@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=production to 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_ENV to "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?

The variables will automatically be pulled from the appropriate environment and development is the default. 
The choice of environment is based on your Babel environment first and if it can't find that your
NPM environment, which should actually be the same, but if the Babel environment
isn't set it uses the NPM environment for robustness.

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-project command you use to clear the cache?

Edit: nvm, I just ran react-native clean-project and 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 = .yourenv

It’s a caching issue. I’ll be doing a bit of maintenance tomorrow 😊

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

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

today i have issue, react native 0.63.3, dotenv 2.5.3, I have ENVFILE=.env.xxx everything is working, assembleDebug, assembleRelease. ( it works ) and then bundleRelease. Testing the release build : $ react-native run-android --variant=release ( fails to pick up env.xxx ) any suggestions or updates for this ?

now react-native run-android --variant=release success 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_ENV to process.env.ENVFILE

yarn start --reset-cache cd android && ./gradlew clean wipe data avd emulator restart the computer

and 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?

@jack828 no worries. fix is in and published as v2.5.1

As noted the 2.5.1 release should help you @ivanichi

thank you. it works

@jack828 no worries. fix is in and published as v2.5.1

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)

module.exports = {
  presets: ['module:metro-react-native-babel-preset'],
  plugins: [
    ["module:react-native-dotenv"]
  ],
};

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: true