toddbluhm / env-cmd

Setting environment variables from a file
https://www.npmjs.com/package/env-cmd
MIT License
1.73k stars 65 forks source link

Concatenation not working #358

Open apuatcfbd opened 1 year ago

apuatcfbd commented 1 year ago
REACT_APP_URL_PROTOCOL=https
REACT_APP_URL=site.com

REACT_APP_BASE_URL="${REACT_APP_URL_PROTOCOL}://${REACT_APP_URL}"

This format was working with CRA but this is not working with env-cmd,

used like "build:test": "env-cmd -f .env.production.test react-app-rewired build"

Is there any way to make this work? Concatenation is important for my case

pjetrucha commented 1 year ago

@apuatcfbd As a workaround, put a variable with concatenation (REACT_APP_BASE_URL) in env files that are default for CRA, such as: https://create-react-app.dev/docs/adding-custom-environment-variables/#what-other-env-files-can-be-used

apuatcfbd commented 1 year ago

Thanks @pjetrucha but I didn't get it. I've different env's for different stages. in that scenario how do I do that? I need this (for same env's vars): https://create-react-app.dev/docs/adding-custom-environment-variables/#expanding-environment-variables-in-env

pjetrucha commented 1 year ago

The CRA with env-cmd merges all the files together, so in your example, it will search for:

.env.production.test, .env.production.local, .env.local, .env.production, .env

The first file is from env-cmd, so variable expansion doesn't work in it, but you can move it to one of 4 other files that CRA will look for, e.g.:

# .env.production.test
REACT_APP_URL_PROTOCOL=https
REACT_APP_URL=site.com

# .env
REACT_APP_BASE_URL="${REACT_APP_URL_PROTOCOL}://${REACT_APP_URL}"