env-cmd icon indicating copy to clipboard operation
env-cmd copied to clipboard

expand local variables not working.

Open GeorgiosP opened this issue 5 years ago • 6 comments

running into an issue with expanding local variables inside an env file. working in v9.0.3 with this setup but seems to fail once I've moved over to a new CRA with env-cmd version 10.1.0

Request URL: http://localhost:5000/$REACT_APP_API_ENV_ORIGIN/

.env file:

REACT_APP_API_ENV_ORIGIN=someAPIURL.com
REACT_APP_API_ENV=$REACT_APP_API_ENV_ORIGIN

package.json:

  "scripts": {
    "build:integration": "env-cmd -f .env.integration craco build"
   }

GeorgiosP avatar Feb 27 '20 22:02 GeorgiosP

Wait are you saying that expanding local variables inside of a .env file worked in v9.0.3?

I honestly do not remember ever implementing that feature. It has been requested before, but I have not gotten around to implementing it yet. Someday soon hopefully!

Currently, env expansion only works for arguments passed on the command line.

This is sort of a duplicate of #97

toddbluhm avatar Mar 03 '20 23:03 toddbluhm

I might be using the wrong term here but let me expand on my example with the above snippet. it might be up the chain and not related to env-cmd but I'm not 100% sure on that. I'll need to do some more research.

The fix i've done for now is just assigning the url directly instead of referencing it within my env file.

call the following env var inside my react app in v9.0.3 with craco and node version v10.15.3

process.env.REACT_APP_API_ENV/slug

network requests will map to

someAPIURL.com/slug

same code with v10.1.0 within a react app and craco on node v10.15.3

maps to the following and fails

$REACT_APP_API_ENV_ORIGIN/slug

GeorgiosP avatar Mar 04 '20 16:03 GeorgiosP

Interesting...could you create a small functioning repo project demonstrating this issue that I can use to test with? I know it is a bit more work, but it would really help!

Together, hopefully we can get this issue resolved 😄

toddbluhm avatar Mar 12 '20 08:03 toddbluhm

This would be a great feature.

@GeorgiosP Just tried this on v9.0.3 and the var was not expanded, so it may be up the tool chain somewhere. Not sure about CRA, but Next.js does this out-of-the-box.

ryanfitzer avatar Aug 21 '20 22:08 ryanfitzer

Possibly fixed in: https://github.com/toddbluhm/env-cmd/pull/253

nkrul avatar Mar 10 '21 03:03 nkrul

you can use dotenvx for this.

HELLO="World"
HELLO2=$HELLO
// index.js
console.log(`Hello ${process.env.HELLO}`)
console.log(`Hello ${process.env.HELLO2}`)
dotenvx run -- node index.js

motdotla avatar Feb 12 '24 05:02 motdotla