andrewmclagan/react-env

How to pick specific .env file?

Closed this issue · 3 comments

I've read the documentation and looked at the example CRA app but I can't figure out how to pinpoint a specific .env file.

I have a brand new CRA with the following file/folder structre

image

I then updated my package.json with the following...

  "scripts": {
    "start": "react-env --env staging -- react-scripts start"

I was expecting this to point to my .env.staging file when doing npm start but it doesn't. Instead it just loads the .env file.

I also tried react-env --env APP_ENV=staging -- react-scripts start but it didn't work either.

Where am I going wrong here?

This is the output from my npm start

$ npm start

> reactenvtest@0.1.0 start E:\Repos\ReactEnvTest\reactenvtest
> react-env --env staging

Writing runtime env  E:\Repos\ReactEnvTest\reactenvtest/public/__ENV.js
{
  "REACT_APP_CRA": "Create React App",
  "REACT_APP_NEXT": "Next.js",
  "REACT_APP_NOT_SECRET_CODE": "1234"
}

And this is the contents of my env.staging

REACT_APP_NEXT="Staging Next.js"
REACT_APP_CRA="Staging Create React App"
REACT_APP_NOT_SECRET_CODE="12345"

To read in a specific file via an argument you would use the --path flag e.g. to read in .env.staging

react-env --path .env.staging 

You are using the --env flag which looks for an existing environment variable by that name and then finds the env file from that name. e.g. APP_ENV=production along with --env APP_ENV would load .env.production

@andrewmclagan ,
it would be better if you make it clear that we should set APP_ENV as an environment variable in terminal.

image

because for now with the example you've written, we might be confused as "you have to replace 'APP_ENV' with the name of environment you want". (As a result, we might end up writing react-env --env staging instead of APP_ENV=staging react-env --env APP_ENV.)

fair call...! thank you. Would you mind writing up a small PR on the docs?