1

How to use .env in react . I can get access to .env.development and .env.production with

 "start":"react-scripts start",
 "build": "react-scripts build",

How to get access to another like .env.staging ?

I gave like this

"build_staging": "set REACT_APP_ENV=staging & react-scripts build",

but not working.

Any suggestions please.

3
  • What operating system are you on? Commented May 25, 2018 at 6:23
  • I am using mac, but it should work for windows also. Commented May 25, 2018 at 6:31
  • I've added an answer please see if that helps, also upvote and accept if it does :) Commented May 25, 2018 at 6:34

3 Answers 3

5

To keep things consistent across linux(my production server) and windows(my development server) I use cross-env

npm install --save cross-env

and my scripts look like this

"scripts": {
    "dev": "cross-env NODE_ENV=development node server",
    "build": "cross-env NODE_ENV=production next build ",
    "start": "cross-env NODE_ENV=production node server"
  },

so to set a custom env like REACT_APP_ENV you'll need to

"build_staging": "cross-env REACT_APP_ENV=staging react-scripts build",

and you can access it in your javascript code using

process.env.REACT_APP_ENV

also to start a staging server you might want to add

"start_staging": "cross-env REACT_APP_ENV=staging react-scripts start"

more about this here

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

5 Comments

It is taking development even after giving this -> "build_staging": "cross-env REACT_APP_ENV=staging react-scripts build",
you might need to add it while starting the app too like "start": "cross-env REACT_APP_ENV=staging react-scripts start",
Yeah you need to add to your start script or better add a new script like "start_staging": "cross-env REACT_APP_ENV=staging react-scripts start", to start a staging server.
Build_staging command is not working ,it is showing as development only. start_staging is showing as staging.
0

[CONTEXT] - I've created React project with Create React. - Running on Windows 10 - Using VSCode IDE - I have the file .env.development on above /src folder. - My code has console.log(process.NODE_ENV) and console.log(process.REACT_APP_YOUR_KEY)


[PROBLEM] When I'm running the program with npm start, the browser prints 'development' for NODE_ENV, but for my React .env variable, it prints undefined.

I try to run npm start with changing the default script of package.json to this start script: set REACT_APP_YOUR_KEY && react-scripts start. Then there isn't any undefined, all works well. 🤨


[SOLUTION] The cause was that the file .env.development is not detected correctly. My hypothesis is the environment development file, Windows or VSCode don't detect well.

Windows File Explorer files

VS Code explorer. Look up the icon of the files 🤔

You have to change the file name from .env.development to .env.

Comments

0

[SOLUTION]: I've created env.js as shown below. env.js placement

after that i have added in index.html the script below

my env.js content

NB: i can acces my env.js var without import them (it seems the best way for me)

2 Comments

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
Please do not post code images copy/past the code in the answer for a better lisibility

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.