Home > Software design >  How to tell React which .env file to use?
How to tell React which .env file to use?

Time:12-20

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?

CodePudding user response:

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"
      }

CodePudding user response:

Create a new scripts key and use REACT_APP_ENV :

 "scripts": {
    "dev": "react-scripts start",
    "start": "serve -s build",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject",
    "prod" : "REACT_APP_ENV=prod npm run dev"
  },

  • Related