You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: packages/react-scripts/template/README.md
+40-21
Original file line number
Diff line number
Diff line change
@@ -400,8 +400,8 @@ to `process.env.NODE_ENV`.
400
400
These environment variables can be useful for displaying information conditionally based on where the project is
401
401
deployed or consuming sensitive data that lives outside of version control.
402
402
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>`:
405
405
406
406
```jsx
407
407
render() {
@@ -416,8 +416,35 @@ render() {
416
416
}
417
417
```
418
418
419
+
During the build, `process.env.REACT_APP_SECRET_CODE` will be replaced with the current value of the `REACT_APP_SECRET_CODE` environment variable. Remember that the `NODE_ENV` variable will be set for you automatically.
420
+
421
+
When you load the app in the browser and inspect the `<input>`, you will see its value set to `abcdef`, and the bold text will show the environment provided when using `npm start`:
422
+
423
+
```html
424
+
<div>
425
+
<small>You are running this application in<b>development</b> mode.</small>
426
+
<form>
427
+
<input type="hidden" value="abcdef"/>
428
+
</form>
429
+
</div>
430
+
```
431
+
432
+
Having access to the `NODE_ENV` is also useful for performing actions conditionally:
433
+
434
+
```js
435
+
if (process.env.NODE_ENV!=='production') {
436
+
analytics.disable();
437
+
}
438
+
```
439
+
419
440
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:
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.
421
448
422
449
#### Windows (cmd.exe)
423
450
@@ -433,29 +460,21 @@ set REACT_APP_SECRET_CODE=abcdef&&npm start
433
460
REACT_APP_SECRET_CODE=abcdef npm start
434
461
```
435
462
436
-
> Note: Defining environment variables in this manner is temporary for the life of the shell session. Setting
437
-
permanent environment variables is outside the scope of these docs.
463
+
### Adding Development Environment Variables In `.env`
438
464
439
-
With our environment variable defined, we start the app and consume the values. Remember that the `NODE_ENV`
440
-
variable will be set for you automatically. When you load the app in the browser and inspect the `<input>`, you will see
441
-
its value set to `abcdef`, and the bold text will show the environment provided when using `npm start`:
465
+
>Note: this feature is available with `react-scripts@0.5.0` and higher.
442
466
443
-
```html
444
-
<div>
445
-
<small>You are running this application in<b>development</b> mode.</small>
446
-
<form>
447
-
<input type="hidden" value="abcdef"/>
448
-
</form>
449
-
</div>
467
+
To define permanent environment vairables, create a file called `.env` in the root of your project:
468
+
469
+
```
470
+
REACT_APP_SECRET_CODE=abcdef
450
471
```
451
472
452
-
Having access to the `NODE_ENV` is also useful for performing actions conditionally:
473
+
These variables will act as the defaults if the machine does not explicitly set them.
474
+
Please refer to the [dotenv documentation](https://github.com/motdotla/dotenv) for more details.
453
475
454
-
```js
455
-
if (process.env.NODE_ENV!=='production') {
456
-
analytics.disable();
457
-
}
458
-
```
476
+
>Note: If you are defining environment variables for development, your CI and/or hosting platform will most likely need
477
+
these defined as well. Consult their documentation how to do this. For example, see the documentation for [Travis CI](https://docs.travis-ci.com/user/environment-variables/) or [Heroku](https://devcenter.heroku.com/articles/config-vars).
0 commit comments