0

I have a React app which is built inside a Docker container when deployed. It all works as it should, no issues. I have an .env file for development with an API_URL variable and an .env.prod file for deployment with a different API_URL. The .env files get passed directly inside the start/build scripts, like this (for yarn start):

"start": "cross-env NODE_ENV=development env-cmd .env node server"

So, the API_URL variable is inside the .env file used in this script. My question is - can I somehow pass the variable to yarn start or yarn build? When I tried, for testing, adding the variable directly in the script, it didn't get picked up, like so: "start": "cross-env NODE_ENV=development API_URL="my api url" env-cmd .env node server".

1 Answer 1

1

The usage is correct except that env_cmd is overriding that key.

You should use the --no-override flag so it won't.

The final line should like: API_URL="my api url" env_cmd .env --no-override node server"

Sign up to request clarification or add additional context in comments.

3 Comments

This sounds like a solution to half of my problem - I still need a way to pass the variable directly from the terminal. Pseudocode: yarn start VARIABLE=value. So if I add --no-override to the script itself inside package.json, can I somehow pass the variable from the terminal?
I tried: cross-env VARIABLE=value yarn start but it didn't get picked up
I forgot to add the new variable to process.env. Your solution with the --no-override flag is correct. On top of that, the line above worked directly from the terminal.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.