Create-react-app: [feature] Implement dotenv-expand to accept variable expansion in dot env files

Created on 19 May 2017  路  8Comments  路  Source: facebook/create-react-app

Currently we use the dotenv library to parse a .env file in the project root and populate process.env. Great!

Although there exists scenarios in large scale deployments where the same environment variables are used accross many "app" or "services" or "containers"... For example

We have websites, API services and databases consuming a base set of environment variables such as:

bash CDN STRIPE_KEY API_HOST VERSION_TAG

We don't want to have to add another set to this simply with the leading key REACT_APP_XXX. Dotenv has a has a sister project / plugin called dotenv-expand that allows, wait for it.... variable expansion in the file! e.g.

bash REACT_APP_CDN=${CDN} REACT_APP_STRIPE_KEY=${STRIPE_KEY} REACT_APP_API_HOST=${API_HOST} REACT_APP_VERSION_TAG=${VERSION_TAG}

proposal

Most helpful comment

This is out in [email protected]! Please let us know if something doesn鈥檛 quite work.
https://github.com/facebookincubator/create-react-app/releases/tag/v1.1.0

All 8 comments

:no_pedestrians: Keep in mind
.env

DB_PASSWORD=test1234

```js
console.log(process.env.NODE_ENV);

Result will be
```js
console.log({"DB_PASSWORD":"test1234", "NODE_ENV":"production"}.NODE_ENV)

Also particularly useful for accessing properties in package.json, which are exposed as npm_package_* environment variables. For examples, npm_package_version or npm_package_name. For a more complete list, see this gist.

Json loader will do this

Sure - they are many ways to load a json file, however, most will expose the entirety of the file to the build bundle (see #2466). Method requested here and fulfilled by linked PR will allow cherry picking which vars get exposed.

This looks neat. I wouldn't mind taking a PR for this, but I'll close this issue since we likely won't add this ourselves.

This is out in [email protected]! Please let us know if something doesn鈥檛 quite work.
https://github.com/facebookincubator/create-react-app/releases/tag/v1.1.0

FYI, to include an underscore in your expression use the ${ } format in your .env:

// bad
FOO_BAR=$npm_package_dependencies_foo_$npm_package_dependencies_bar
// 2.0

// good
FOO_BAR=${npm_package_dependencies_foo}_${npm_package_dependencies_bar}
// 1.0_2.0

where

// package.json
  "dependencies" : {
    "foo": "1.0",
    "bar": "2.0"
   }
Was this page helpful?
0 / 5 - 0 ratings

Related issues

Evan-GK picture Evan-GK  路  3Comments

stopachka picture stopachka  路  3Comments

adrice727 picture adrice727  路  3Comments

ap13p picture ap13p  路  3Comments

JimmyLv picture JimmyLv  路  3Comments