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

environment variables not being set #116

Open
patrickjames242 opened this issue Sep 14, 2022 · 3 comments
Open

environment variables not being set #116

patrickjames242 opened this issue Sep 14, 2022 · 3 comments

Comments

@patrickjames242
Copy link

In my nx expo project, the process.env object shows the following

{
"NODE_ENV": "development",
}

None of the variables in my .env file are being included.

@leggomuhgreggo
Copy link

https://docs.expo.dev/guides/environment-variables/

@BlumDev92
Copy link

I'm also facing this problem. I've read docs of @leggomuhgreggo , but I don't know how to configure this in theproject.json.
It seems that I can't add env variables to the@nrwl/expo:startexecutor. Any help/ideas?

@patrickjames242
Copy link
Author

@BlumDev92 Here's how I fixed the problem. I changed my app.json to app.config.js and added the following code.

module.exports = {
  expo: {
    //...
    extra: {
      env: getNXVariables(),
    },
  },
};

/**
 * Nx isn't passing env variables to expo so we need to add them to the extra section of the config
 * then manually set process.env when our app starts
 *
 */
function getNXVariables() {
  let result = {};
  for (const key in process.env) {
    if (key.startsWith('NX_')) {
      result[key] = process.env[key];
    }
  }
  return result;
}

Then run expo install expo-constants and add the following code to your javascript before any other code is executed

import Constants from 'expo-constants';

(function setEnvironmentVariables() {
  Object.assign(process.env, Constants.manifest?.extra?.env);
})();

Now you can use process.env.NX_ANYTHING to your heart's delight!

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

3 participants