Skip to content

Commit 020c400

Browse files
committed
Document loading environment variables in .env file
1 parent b8a191b commit 020c400

File tree

1 file changed

+40
-23
lines changed

1 file changed

+40
-23
lines changed

Diff for: packages/react-scripts/template/README.md

+40-23
Original file line numberDiff line numberDiff line change
@@ -400,8 +400,8 @@ to `process.env.NODE_ENV`.
400400
These environment variables can be useful for displaying information conditionally based on where the project is
401401
deployed or consuming sensitive data that lives outside of version control.
402402
403-
First, you need to have environment variables defined, which can vary between OSes. For example, let's say you wanted to
404-
consume a secret defined in the environment inside a `<form>`:
403+
First, you need to have environment variables defined. For example, let's say you wanted to consume a secret defined
404+
in the environment inside a `<form>`:
405405
406406
```jsx
407407
render() {
@@ -416,27 +416,6 @@ render() {
416416
}
417417
```
418418
419-
The above form is looking for a variable called `REACT_APP_SECRET_CODE` from the environment. In order to consume this
420-
value, we need to have it defined in the environment:
421-
422-
#### Windows (cmd.exe)
423-
424-
```cmd
425-
set REACT_APP_SECRET_CODE=abcdef&&npm start
426-
```
427-
428-
(Note: the lack of whitespace is intentional.)
429-
430-
#### Linux, OS X (Bash)
431-
432-
```bash
433-
REACT_APP_SECRET_CODE=abcdef npm start
434-
```
435-
436-
> Note: Defining environment variables in this manner is temporary for the life of the shell session. Setting
437-
permanent environment variables in development can be done in a `.env` file in the root of your project.
438-
[dotenv](https://github.com/motdotla/dotenv) takes care of loading these for you.
439-
440419
With our environment variable defined, we start the app and consume the values. Remember that the `NODE_ENV`
441420
variable will be set for you automatically. When you load the app in the browser and inspect the `<input>`, you will see
442421
its value set to `abcdef`, and the bold text will show the environment provided when using `npm start`:
@@ -458,6 +437,44 @@ if (process.env.NODE_ENV !== 'production') {
458437
}
459438
```
460439
440+
The above form is looking for a variable called `REACT_APP_SECRET_CODE` from the environment. In order to consume this
441+
value, we need to have it defined in the environment. This can be done using two ways: either in your shell or in
442+
a `.env` file.
443+
444+
### Adding Temporary Environment Variables In Your Shell
445+
446+
Defining environment variables can vary between OSes. It's also important to know that this manner is temporary for the
447+
life of the shell session.
448+
449+
#### Windows (cmd.exe)
450+
451+
```cmd
452+
set REACT_APP_SECRET_CODE=abcdef&&npm start
453+
```
454+
455+
(Note: the lack of whitespace is intentional.)
456+
457+
#### Linux, OS X (Bash)
458+
459+
```bash
460+
REACT_APP_SECRET_CODE=abcdef npm start
461+
```
462+
463+
### Adding Development Environment Variables In `.env`
464+
465+
>Note: this feature is available with `react-scripts@0.5.0` and higher.
466+
467+
Defining permanent environment variables in development can be done in a `.env` file in the root of your project.
468+
[dotenv](https://github.com/motdotla/dotenv) takes care of loading these for you.
469+
470+
```bash
471+
echo "REACT_APP_SECRET_CODE=abcsdef" >> .env
472+
npm start
473+
```
474+
475+
>Note: if you are defining environment variables for development your CI and/or hosting platform will most likely need
476+
these defined as well. Consult their documentation how to do this. Eg. [Travis CI](https://docs.travis-ci.com/user/environment-variables/) or [Heroku](https://devcenter.heroku.com/articles/config-vars)
477+
461478
## Can I Use Decorators?
462479
463480
Many popular libraries use [decorators](https://medium.com/google-developers/exploring-es7-decorators-76ecb65fb841) in their documentation.

0 commit comments

Comments
 (0)