2

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
  • npmjs.com/package/env-cmd#using-custom-env-file-path Commented Jul 6, 2020 at 11:06
  • Thanks, Is there any way to do this from webpack, so that it can pick environment folder directly, just like it pics from the root directory? Commented Jul 6, 2020 at 11:10

2 Answers 2

1

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')
Sign up to request clarification or add additional context in comments.

Comments

1

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
}

Comments

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.