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

In production Release failed - env variable not importing #38

Closed
2manoj1 opened this issue Jul 30, 2020 · 68 comments
Closed

In production Release failed - env variable not importing #38

2manoj1 opened this issue Jul 30, 2020 · 68 comments
Labels
wontfix This will not be worked on

Comments

@2manoj1
Copy link

2manoj1 commented Jul 30, 2020

On Production build ios React Native 0.63.2 build failed. From @env variable not importing.

@goatandsheep
Copy link
Owner

What is the error? What does your .babelrc look like?

@goatandsheep
Copy link
Owner

If 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

@goatandsheep goatandsheep added the waiting author response When a ticket has been addressed but you're waiting for the author to confirm the fix. label Jul 30, 2020
@2manoj1
Copy link
Author

2manoj1 commented Jul 30, 2020

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 goatandsheep added bug Something isn't working and removed waiting author response When a ticket has been addressed but you're waiting for the author to confirm the fix. labels Jul 30, 2020
@goatandsheep
Copy link
Owner

Related to tusbar/babel-plugin-dotenv-import#164

@oleksandr-dziuban
Copy link

oleksandr-dziuban commented Aug 6, 2020

@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

@AndriyF
Copy link

AndriyF commented Aug 6, 2020

@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
Copy link
Owner

@AndriyF do you think allowUndefined: true should be set as default?

@AndriyF
Copy link

AndriyF commented Aug 6, 2020

@goatandsheep there are no restrictions set by default, so, I think, for the sake for consistency it's better to have allowUndefined: true

goatandsheep added a commit that referenced this issue Aug 6, 2020
* change default `allowUndefined` from `false` to `true`
* progress towards #38 but not a fix
@goatandsheep
Copy link
Owner

goatandsheep commented Aug 6, 2020

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

@2manoj1
Copy link
Author

2manoj1 commented Aug 6, 2020

@goatandsheep check this. In code two dotenv config. can you check this.
Screenshot 2020-08-06 at 10 32 36 PM

@goatandsheep
Copy link
Owner

goatandsheep commented Aug 6, 2020

@2manoj1 Thank you but I don't think that is it. The dotenv creators suggest it here: ( motdotla/dotenv#390 (comment) ). Also, the issue is also present in the original fork #38 (comment) . However, I think we should change the multi-env strategy. See #1

@goatandsheep
Copy link
Owner

@2manoj1 is this also affecting dev builds like ticket #52

@2manoj1
Copy link
Author

2manoj1 commented Aug 12, 2020

No. In Dev build it was worked as expected.

@goatandsheep
Copy link
Owner

goatandsheep commented Aug 12, 2020

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.

@ev3nst
Copy link

ev3nst commented Aug 25, 2020

for those that has empty variables in release builds. just change ur env like this -->

from
API_URL=YOUR_API_URL

to
API_URL='YOUR_API_URL'

@goatandsheep
Copy link
Owner

@BurakKartal1 huh. that's interesting. I wonder why that works. Should the library cast the values to strings?

@MrtYlnzz
Copy link

I am having same issue on 0.63.2 any update on this?

@info-bit
Copy link

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.

@goatandsheep
Copy link
Owner

goatandsheep commented Aug 31, 2020

@2manoj1 Can you try again with the new release and let me know if that works?

@MrtYlnzz
Copy link

MrtYlnzz commented Sep 1, 2020

@goatandsheep I have tested on 2.4.0 and it seems to have not solved the issue for me.

@goatandsheep
Copy link
Owner

@MrtYlnzz thank you for checking

@chmiiller
Copy link

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 =)

@oalcantarpcore
Copy link

oalcantarpcore commented Sep 10, 2020

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",

@Slals
Copy link

Slals commented Sep 20, 2020

Hi, any news on this issue?

xCode build get error is not defined in .env when building release, it works in debug mode. I use multi-env and tried to create a .env (I use .env.production in normal condition).

"react-native": "0.63.2",
"react-native-dotenv": "^2.4.0",

@zhahaoyu
Copy link

If I move .env and .env.production to ios folder, then the problem is solved.

@ivanichi
Copy link

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 ?

@goatandsheep
Copy link
Owner

Hi @ivanichi this library is not checking for ENVFILE to my knowledge. Also release should be <path from babel config>.production. Multi-env is inspired by dotenv-flow. You can also book me to take a look at your project to make some suggestions https://issuehunt.io/r/goatandsheep/react-native-dotenv/issues/38

I'd also recommend if there are missing values that you clear the cache: yarn start --reset-cache

@zhahaoyu
Copy link

@goatandsheep will there be a fix so we don't need to copy the .env files to the ios folder?

@goatandsheep
Copy link
Owner

Hm...do you think this is a simple path fix? I'm not having this issue on my end, so I will not release until fully tested, but can you test the following branch?

  "devDependencies": {
    "react-native-dotenv": "git+https://git@github.com/goatandsheep/react-native-dotenv.git#bugfix/gh-38"
  }

@ivanichi
Copy link

@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

@ivanichi
Copy link

ivanichi commented Feb 23, 2021

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
Copy link
Owner

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

@ivanichi
Copy link

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.

@goatandsheep
Copy link
Owner

https://paypal.me/kemalahmedmac

@gabrieljoelc
Copy link

@ivanichi

we've had the issue of needing to restart the computer when changing values in the .env.

does anyone know why you have to restart the computer?

@goatandsheep
Copy link
Owner

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

@ivanichi
Copy link

ivanichi commented Apr 1, 2021

@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

@gabrieljoelc
Copy link

@ivanichi thanks.

Yes the restarts have consistently worked but it's pretty painful when switching between environments for test and prod builds

@alizahid
Copy link

alizahid commented Apr 3, 2021

I'm able to get this working reliably by having two env files; .env.development and .env.production as specified in the docs and not having .env at all.

@goatandsheep
Copy link
Owner

The solution is now to install https://www.npmjs.com/package/react-native-clean-project and clean using that. No more restarts.

@gabrieljoelc
Copy link

gabrieljoelc commented Apr 12, 2021

@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!

@stale
Copy link

stale bot commented Jun 11, 2021

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the wontfix This will not be worked on label Jun 11, 2021
@stale stale bot closed this as completed Jun 18, 2021
@brianwachira
Copy link

The solution is now to install https://www.npmjs.com/package/react-native-clean-project and clean using that. No more restarts.

I have tried this on React Native v0.70.3 and it is not working.
I have 2 .env files, .env.development and .env.production
Here is my babel.config.js

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, }, ], 'react-native-reanimated/plugin', ], };

I have tried all the solutions above and none of them work. I am considering to use another .env package.
@goatandsheep please help?

@goatandsheep
Copy link
Owner

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

@brianwachira
Copy link

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.

@Lauro235
Copy link

Lauro235 commented Nov 4, 2022

Hi, there, I'm having an issue with my variables, showing as undefined.
Here is my babel.config.js

module.exports = function(api) {
  api.cache(true);
  return {
    "presets": ['babel-preset-expo', "module:react-native-dotenv"],
    "plugins": [
      [
        "module:react-native-dotenv", {
        "envName": "APP_ENV",
        "moduleName": "@env",
        "path": ".env",
        "allowUndefined": true,
        }
      ]
    ] 
  };
};

Currently using Node 18.12.0 and NPM 8.19.2

@goatandsheep
Copy link
Owner

Try cache(false)?

@Lauro235
Copy link

Lauro235 commented Nov 4, 2022

No Luck so far. I've tried clearing all the node modules and removing cache. I've also set api.cache(false).. It's not having it :(

@Lauro235
Copy link

Lauro235 commented Nov 4, 2022

Somehow I fixed it.. For me this worked..

module.exports = function(api) {
  api.cache(false);
  return {
    presets: ['babel-preset-expo', "module:metro-react-native-babel-preset"],
    plugins: [
      [
        "module:react-native-dotenv", {
        "envName": "APP_ENV",
        "moduleName": "@env",
        "path": ".env",
        }
      ]
    ] 
  };
};
  "dependencies": {
    "@expo/webpack-config": "^0.16.19",
    "expo": "~46.0.16",
    "expo-splash-screen": "~0.16.2",
    "expo-status-bar": "~1.4.0",
    "react": "18.0.0",
    "react-dom": "18.0.0",
    "react-native": "0.69.6",
    "react-native-auth0": "^2.14.1",
    "react-native-web": "~0.18.7"
  },
  "devDependencies": {
    "@babel/core": "^7.12.9",
    "metro-react-native-babel-preset": "^0.73.3",
    "react-native-dotenv": "^3.3.1"
  },

@odera89
Copy link

odera89 commented Nov 27, 2023

@goatandsheep will there be a fix so we don't need to copy the .env files to the ios folder?

I have the same issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests