Skip to content

Commit b8a191b

Browse files
committed
Load environment file via dotenv if .env file is present
1 parent ba34b0b commit b8a191b

File tree

6 files changed

+22
-1
lines changed

6 files changed

+22
-1
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

+2-1
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,8 @@ REACT_APP_SECRET_CODE=abcdef npm start
434434
```
435435
436436
> 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.
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.
438439
439440
With our environment variable defined, we start the app and consume the values. Remember that the `NODE_ENV`
440441
variable will be set for you automatically. When you load the app in the browser and inspect the `<input>`, you will see

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)