Sunday, August 21, 2022

[FIXED] How to tell React which .env file to use?

Issue

I have a React app that I run with the command:

npm run dev

This launches the app and uses the .env.local file. I want to be able to configure a file called .env.production that will be used instead of .env.local when I run the command npm run prod.

These are my current scripts:

  "scripts": {
    "dev": "react-scripts start",
    "start": "serve -s build",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },

How can I do this?


Solution

install npmjs.com/package/env-cmd

 "scripts": {
        "start": "react-scripts start",
        "start:local": "env-cmd -f ./.env.local react-scripts start",
        "start:Dev": "env-cmd -f ./.env.development react-scripts start",
        "start:QA": "env-cmd -f ./.env.test react-scripts start",
        "build": "react-scripts build",
        "test": "react-scripts test",
        "eject": "react-scripts eject"
      }


Answered By - Ram Rana
Answer Checked By - Clifford M. (PHPFixing Volunteer)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.