1

I am new to react and setting up environment variables for my project. Here is what I did..

  1. added .env-cmdrc.json as follows

    {
     "development":{
     "REACT_APP_BASE_URL": "https://servername:port/"
     },
     "staging":{
     "REACT_APP_BASE_URL": "http://servername:port/"
     },
      "local":{
      "REACT_APP_BASE_URL": "http://localhost:port/"
      }
     }
    
  2. installed npm

npm install env-cmd or npm install -g env-cmd

  1. edited package.json as follows:

     "start:development": "env-cmd -e development react-scripts start",
     "start:staging": "env-cmd -e staging react-scripts start",
     "start:local": "env-cmd -e local react-scripts start",
     "build:development": "env-cmd -e development react-scripts build",
     "build:staging": "env-cmd -e staging react-scripts build",
    
  2. tried - npm run start:development

    was giving me env-cmd error

  3. again ran

    npm install env-cmd

  4. Now tried - npm run start:development

Failed to find .rc file at default paths: [./.env-cmdrc,./.env-cmdrc.js,./.env-cmdrc.json] at getRCFile

I am doing it first time and would appreciate any help..what am I missing here..

2 Answers 2

2

I tried your code, but it works well.
Check your code, ensure the location of your config file .env-cmdrc.json, it should be placed under root dict of your project (the same level with package.json)

Sign up to request clarification or add additional context in comments.

Comments

0

I would suggest using dotenv package instead of env-cmd.

  1. install the package - npm i dotenv
  2. Create a environment file in your root directory - .env
  3. Declare a variable - REACT_APP_URL=http://localhost/....
  4. Use the variable - process.env.REACT_APP_URL

In order to start the React application you need to check your package.json file and make sure it contains something like this:

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

If that's in place you can run the following command: npm start


Now you can start coding :)

2 Comments

How does that switch to staging and production? It looks like this sets to localhost.
@J.Gwinner you can manually switch to production by changing the REACT_APP_URL or you can automate that with a bash script.

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.