Environment variables not working and not getting unset

Hi!

I have set one environment variable for each environment (dev, stg, prod) with the same key but different values.
In the Forge CLI I see the different values when I check variables from environments but when I see the app I get back one value for all 3 environment, which is a problem.

Is it normal? What am I doing wrong?

An another interesting thing, if I unset a variable, and it disappear from Forge CLI, the app still reach this deleted variable. (Even after run a build and a deploy command too.)
Is there any time to wait until delete happen or what can be the problem?

@ccurti any idea for this?
Thanks for your help in advance!

Hi @kornel.leitner,

About the variables having the same value in all 3 environments, I just did a test and I could see the different value in my app for each environment.

Are you sure that you are checking the right environment of the app?
You’ll see a lozenge next to the app link showing the environment:
image

Regarding unsetting a variable with the forge variables unset MY_KEY command for the development environment and the forge variables unset MY_KEY -e staging for staging, I had to deploy the app before the variable was not able available.

Let us know,
Caterina

1 Like

Hi Caterina!

Thanks for your answer.
Seems I tried to set NODE_ENV as key in Forge CLI and as I read with process.env.NODE_ENV command the value was always ‘production’ in the React APP.

But the real problem is, I set another key in Forge CLI with the command below:
forge variables set TEST_VAR hello

I see it is set in the development environment, when check with: forge variables list

After deploy, I try to reach this variable in our Custom UI React app, calling this in the App.js:
process.env.TEST_VAR
and the result is undefined.

Is it possible to reach Forge CLI environment variables in React app somehow?

Found something about snapshot context they are unavailable, but with runtime: snapshots: false in the manifest.yml the result was the same.

1 Like

Hi @ccurti and @kornel.leitner

I’m also experiencing this issue.

Environment variables appear when I run forge variables list but don’t get passed in to my deployed development environment (using forge deploy, not forge tunnel). They all return undefined.

Also, just as the commenter above, process.env.NODE_ENV sometimes returns “production” when in development. Though this doesn’t happen every time.

Hi all, I think the issue here is attempting to read the variable via process.env from custom UI - it was only designed to work from forge functions (such as UI Kit resolvers). See Trying to add .env to a forge Custom UI app - #2 by dmorrow for more.

Custom UI code is run in the browser. What process.env actually returns when you call it depends on what polyfill the bundler has set up, because there’s no global process variable available in the browser. Normally the bundler will substitute whatever environment variable values it can see while it’s performing the bundling.

You could invoke a forge function and have it retrieve the variable values of interest for you, but if you want to avoid the network call another thing you could try is wrapping your deployment step in a script that pulls down the current forge variable values and sets them somewhere that the bundler can find them (either in the currently executing environment or some kind of property file).

3 Likes

Thanks for your answer!
Now I see the problem clearly.
dotenv-cli was the perfect solution for us, to handle different environment variables in our project.

1 Like