Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feature] Implement dotenv-expand to accept variable expansion in dot env files #2223

Closed
andrewmclagan opened this issue May 19, 2017 · 8 comments

Comments

@andrewmclagan
Copy link

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:

  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.

REACT_APP_CDN=${CDN}
REACT_APP_STRIPE_KEY=${STRIPE_KEY}
REACT_APP_API_HOST=${API_HOST}
REACT_APP_VERSION_TAG=${VERSION_TAG}
@FDiskas
Copy link

FDiskas commented Sep 7, 2017

🚷 Keep in mind
.env

DB_PASSWORD=test1234
console.log(process.env.NODE_ENV);

Result will be

console.log({"DB_PASSWORD":"test1234", "NODE_ENV":"production"}.NODE_ENV)

moos added a commit to moos/create-react-app that referenced this issue Nov 2, 2017
@moos
Copy link
Contributor

moos commented Nov 2, 2017

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.

@FDiskas
Copy link

FDiskas commented Nov 2, 2017

Json loader will do this

@moos
Copy link
Contributor

moos commented Nov 12, 2017

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.

@gaearon
Copy link
Contributor

gaearon commented Jan 8, 2018

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.

@gaearon gaearon closed this as completed Jan 8, 2018
@gaearon
Copy link
Contributor

gaearon commented Jan 9, 2018

I merged #3387.

gaearon pushed a commit that referenced this issue Jan 9, 2018
#3387)

* fix #2223 - [feature] Implement dotenv-expand to accept variable expansion in dot env files

* add to README TOC

* fix readme

* Update README.md
@gaearon
Copy link
Contributor

gaearon commented Jan 15, 2018

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

@moos
Copy link
Contributor

moos commented Apr 13, 2018

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"
   }

Pavek pushed a commit to Pavek/create-react-app that referenced this issue Jul 10, 2018
…ble expa… (facebook#3387)

* fix facebook#2223 - [feature] Implement dotenv-expand to accept variable expansion in dot env files

* add to README TOC

* fix readme

* Update README.md
zmitry pushed a commit to zmitry/create-react-app that referenced this issue Aug 14, 2018
…ble expa… (facebook#3387)

* fix facebook#2223 - [feature] Implement dotenv-expand to accept variable expansion in dot env files

* add to README TOC

* fix readme

* Update README.md
@lock lock bot locked and limited conversation to collaborators Jan 19, 2019
LishuGupta652 pushed a commit to LishuGupta652/create-react-app-1 that referenced this issue Oct 8, 2024
…ble expa… (facebook#3387)

* fix facebook#2223 - [feature] Implement dotenv-expand to accept variable expansion in dot env files

* add to README TOC

* fix readme

* Update README.md
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants