Skip to content

Commit 8e5183a

Browse files
ayrtongaearon
authored andcommitted
Load environment file in development (#695)
* Load environment file via dotenv if .env file is present * Document loading environment variables in .env file * Minor doc tweaks
1 parent 2050174 commit 8e5183a

File tree

6 files changed

+60
-21
lines changed

6 files changed

+60
-21
lines changed

packages/react-scripts/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
"cross-spawn": "4.0.0",
3535
"css-loader": "0.24.0",
3636
"detect-port": "1.0.0",
37+
"dotenv": "2.0.0",
3738
"eslint": "3.5.0",
3839
"eslint-config-react-app": "0.2.0",
3940
"eslint-loader": "1.5.0",

packages/react-scripts/scripts/build.js

+6
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@
1212
// Do this as the first thing so that any code reading it knows the right env.
1313
process.env.NODE_ENV = 'production';
1414

15+
// Load environment variables from .env file. Surpress warnings using silent
16+
// if this file is missing. dotenv will never modify any environment variables
17+
// that have already been set.
18+
// https://github.com/motdotla/dotenv
19+
require('dotenv').config({silent: true});
20+
1521
var chalk = require('chalk');
1622
var fs = require('fs-extra');
1723
var path = require('path');

packages/react-scripts/scripts/start.js

+6
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@
1111

1212
process.env.NODE_ENV = 'development';
1313

14+
// Load environment variables from .env file. Surpress warnings using silent
15+
// if this file is missing. dotenv will never modify any environment variables
16+
// that have already been set.
17+
// https://github.com/motdotla/dotenv
18+
require('dotenv').config({silent: true});
19+
1420
var path = require('path');
1521
var chalk = require('chalk');
1622
var webpack = require('webpack');

packages/react-scripts/scripts/test.js

+6
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@
1010
process.env.NODE_ENV = 'test';
1111
process.env.PUBLIC_URL = '';
1212

13+
// Load environment variables from .env file. Surpress warnings using silent
14+
// if this file is missing. dotenv will never modify any environment variables
15+
// that have already been set.
16+
// https://github.com/motdotla/dotenv
17+
require('dotenv').config({silent: true});
18+
1319
const createJestConfig = require('./utils/createJestConfig');
1420
const jest = require('jest');
1521
const path = require('path');

packages/react-scripts/template/README.md

+40-21
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, lets say you wanted to consume a secret defined
404+
in the environment inside a `<form>`:
405405
406406
```jsx
407407
render() {
@@ -416,8 +416,35 @@ render() {
416416
}
417417
```
418418
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+
419440
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.
421448
422449
#### Windows (cmd.exe)
423450
@@ -433,29 +460,21 @@ set REACT_APP_SECRET_CODE=abcdef&&npm start
433460
REACT_APP_SECRET_CODE=abcdef npm start
434461
```
435462
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`
438464
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.
442466
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
450471
```
451472
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.
453475
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).
459478
460479
## Can I Use Decorators?
461480

packages/react-scripts/template/gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ build
1111

1212
# misc
1313
.DS_Store
14+
.env
1415
npm-debug.log

0 commit comments

Comments
 (0)