Change Default Path of .env files in Reactjs

Viewed 829

I want to setup development & production environment in ReactJs I have created two files .env.production & .env.development in root

Structure looks like below

root
   |
   .env.production
   .env.development

I want to change this to

 root
       |
       enviroments
                  |
                  .env.production
                  .env.development

In simple words i want to change the default location of .env files in react js. How can i do that please help.

2 Answers

I found the solution after some research, but for this we need to eject

After eject

change following configuration in config/paths.js

dotenv: resolveApp('.env')

to

dotenv: resolveApp('environments/.env')

The easiest way is to use env-cmd for that

npm i env-cmd

and update your package.json file scripts to look like this:

"scripts": {
    "start": "env-cmd -f ../.env  react-scripts start" // replace "../.env" with your relative path
}
Related