Skip to content

Commit

Permalink
Load environment file via dotenv if .env file is present
Browse files Browse the repository at this point in the history
  • Loading branch information
ayrton committed Sep 23, 2016
1 parent ba34b0b commit b8a191b
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 1 deletion.
1 change: 1 addition & 0 deletions packages/react-scripts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"cross-spawn": "4.0.0",
"css-loader": "0.24.0",
"detect-port": "1.0.0",
"dotenv": "2.0.0",
"eslint": "3.5.0",
"eslint-config-react-app": "0.2.0",
"eslint-loader": "1.5.0",
Expand Down
6 changes: 6 additions & 0 deletions packages/react-scripts/scripts/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@
// Do this as the first thing so that any code reading it knows the right env.
process.env.NODE_ENV = 'production';

// Load environment variables from .env file. Surpress warnings using silent
// if this file is missing. dotenv will never modify any environment variables
// that have already been set.
// https://github.com/motdotla/dotenv
require('dotenv').config({silent: true});

var chalk = require('chalk');
var fs = require('fs-extra');
var path = require('path');
Expand Down
6 changes: 6 additions & 0 deletions packages/react-scripts/scripts/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@

process.env.NODE_ENV = 'development';

// Load environment variables from .env file. Surpress warnings using silent
// if this file is missing. dotenv will never modify any environment variables
// that have already been set.
// https://github.com/motdotla/dotenv
require('dotenv').config({silent: true});

var path = require('path');
var chalk = require('chalk');
var webpack = require('webpack');
Expand Down
6 changes: 6 additions & 0 deletions packages/react-scripts/scripts/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@
process.env.NODE_ENV = 'test';
process.env.PUBLIC_URL = '';

// Load environment variables from .env file. Surpress warnings using silent
// if this file is missing. dotenv will never modify any environment variables
// that have already been set.
// https://github.com/motdotla/dotenv
require('dotenv').config({silent: true});

const createJestConfig = require('./utils/createJestConfig');
const jest = require('jest');
const path = require('path');
Expand Down
3 changes: 2 additions & 1 deletion packages/react-scripts/template/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,8 @@ REACT_APP_SECRET_CODE=abcdef npm start
```
> Note: Defining environment variables in this manner is temporary for the life of the shell session. Setting
permanent environment variables is outside the scope of these docs.
permanent environment variables in development can be done in a `.env` file in the root of your project.
[dotenv](https://github.com/motdotla/dotenv) takes care of loading these for you.
With our environment variable defined, we start the app and consume the values. Remember that the `NODE_ENV`
variable will be set for you automatically. When you load the app in the browser and inspect the `<input>`, you will see
Expand Down
1 change: 1 addition & 0 deletions packages/react-scripts/template/gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ build

# misc
.DS_Store
.env
npm-debug.log

0 comments on commit b8a191b

Please sign in to comment.