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

Variable are UNDEFINED on IOS #788

Open
Antholife opened this issue Jan 6, 2024 · 14 comments
Open

Variable are UNDEFINED on IOS #788

Antholife opened this issue Jan 6, 2024 · 14 comments

Comments

@Antholife
Copy link

Antholife commented Jan 6, 2024

I use:

  • "react-native": "0.72.7"
  • "react-native-config": "1.5.1",

When I log my variables they are undefined, I've come to find you because I haven't been able to solve the problem for a few days.

I don't use EXPO.

My .env:
API_ENDPOINT=DEVELOPMENT_PHYSICAL

My react-native.config.d.ts:

declare module "react-native-config" {
export interface NativeConfig {
API_ENDPOINT:
| "DEVELOPMENT"
| "DEVELOPMENT_PHYSICAL"
| "PRODUCTION"
| undefined;
}

export const Config: NativeConfig;
export default Config;
}`

In my file:
import { Config } from "react-native-config";
const { API_ENDPOINT } = Config;

console.log(Config.API_ENDPOINT);
console.log(API_ENDPOINT);

LOG undefined
LOG undefined

@abumostafa1global
Copy link

hey, did you find a way to make it work?

@justincounts
Copy link

I think I am seeing this as well

@sa-ma
Copy link

sa-ma commented Feb 8, 2024

I have the same issue

@sa-ma
Copy link

sa-ma commented Feb 8, 2024

I have the same issue

Solved mine, please clean and build on XCode

@dan-iliescu
Copy link

I've been having this issue for weeks, still no solution in sight.

@vivianmauer
Copy link

Hi @Antholife

You are importing in the wrong way

it should be import Config from "react-native-config";
and not import { Config } from "react-native-config";

@Antholife
Copy link
Author

Antholife commented Feb 16, 2024

Hi @Antholife

You are importing in the wrong way

it should be import Config from "react-native-config"; and not import { Config } from "react-native-config";

Hi,

I've already tried both, which is not the problem, and it's valid.

And some people have the same problem

declare module 'react-native-config' {
export interface NativeConfig {
HOSTNAME?: string;
}

export const Config: NativeConfig
export default Config
}

@ariel-diaz
Copy link

Same issue here

"react-native-config": "^1.5.1"
"react-native": "0.72.7"

@justincounts
Copy link

Not sure if this was a long-running user error on my part, or something changed in react-native-config, but I determined that the shell script I was using to generate .env files during builds was running after the part of the xcode build that looks for the files.

I ended up moving this process from a Build Phase into a pre-build action and my issue was resolved.

@dan-iliescu
Copy link

dan-iliescu commented Feb 27, 2024 via email

@justincounts
Copy link

The Shell Script was invoked as part of a Build Phase Script

However these scripts were running after Pod were compiled, the .env file was generated after react-native-config's build process looked for it.,

I instead invoke the script in a Pre-Build Action

@Dokome
Copy link

Dokome commented Apr 7, 2024

i solved the promblem by this issue on stackoverflow

@edwinwong90
Copy link

Is works for me, this is my RN version

- react-native: 0.74.1
- react-native-config: ^1.5.1

At first, I was encounter the same issue undefined in ios. It works after I "Clean Build Folder". It could be cache issue. Is better try following :

  • Clear Metro Cache
  • Clean Build Folder
  • Clear Xcode Cache

Hope it works for you!

@valerii-link
Copy link

Why?
Currently Xcode doesn't know about .env changes, therefore always rebuilding with cached data.

Temporary workarounds:

  1. Clean/Rebuild on EVERY .env change:
    • Xcode -> Product -> Clean Build Folder OR rm -rf ~/Library/Developer/Xcode/DerivedData in your terminal
    • Build/Run you project

  2. Update "[CP-User] Config codegen" at Build Phases for Pods/react-native-config target:

dotenv-workaround
  1. Patch node-modules/react-native-config/react-native-config.podspec:
    • Patch react-native-config.podspec file with next fix
    cd ios && pods install
    • Build/Run you project

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

10 participants