goatandsheep/react-native-dotenv

Cannot get env variables in rn 0.69

elirangoshen opened this issue · 11 comments

  • Asked question in discussions
  • Tried the troubleshooting Wiki
  • Followed the migration Wiki

Describe the bug
im getting undefined variables in rn 0.69

To Reproduce
Steps to reproduce the behavior:

  1. upgrade to rn 0.69
  2. set env variables
  3. it gets undefined

Expected behavior
that should work

Screenshots
If applicable, add screenshots to help explain your problem.

Smartphone (please complete the following information):

  • Device: Iphone 13
  • OS: iOs 15.5

Additional context
my babel config file :

const presets = ['module:metro-react-native-babel-preset']
const plugins = ['@babel/plugin-proposal-export-namespace-from']

plugins.push([
  'module-resolver',
  {
    root: ['./src'],
    extensions: ['.js', '.json'],
    alias: {
      '@': './src',
    },
  },
  'react-native-reanimated/plugin',
])

plugins.push([
  'module:react-native-dotenv',
  {
    envName: 'APP_ENV',
    moduleName: 'react-native-dotenv',
    path: '.env',
    safe: false,
    allowUndefined: true,
    verbose: false,
  },
])

module.exports = {
  presets,
  plugins,
}

how i try to import it :

import {
  API_BASE_URL,
  CONTENTFUL_ACCESS_TOKEN,
  CONTENTFUL_BASE_URL,
} from 'react-native-dotenv'

Hey, thank you for opening this issue! 🙂 To boost priority on this issue and support open source please tip the team at https://issuehunt.io/r/goatandsheep/react-native-dotenv/issues/354

same on RN 70.1, any solution?

Same problem on 70.1

add babel.config.js

plugins: [
    [
      "module:react-native-dotenv",
      {
        envName: "APP_ENV",
        moduleName: "@env",
        path: ".env"
      }
    ]
  ]

huh...I'm working on debugging some issues with the Babel team and I'm hoping it resolves a bunch of these, but i will also experiment shortly

Is this still an issue?

I'm not seeing the issue to be honest, but i'm assuming it's a caching issue

Can you try again with latest version?

From the documentation is not clear at which moment the env vars get populated.

If you're trying to use the env var at the top level in a file (e.g. right after imports), the value will come up as undefined.
Is there a specific rule with that while using this library?

Can you show me the error? I will create a how it works section I guess haha

Thanks for the fast reply, so it turns out that the issue was the fact that I didn't restart the Metro server after configuring the plugin😅. It works fine now.