Skip to content

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

Closed

Description

@andrewmclagan

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}

Activity

  1. FDiskas commented on Sep 7, 2017

    @FDiskas

    🚷 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)
  2. added a commit that references this issue on Nov 2, 2017
    00ce686
  3. moos commented on Nov 2, 2017

    @moos
    Contributor

    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.

  4. FDiskas commented on Nov 2, 2017

    @FDiskas

    Json loader will do this

  5. moos commented on Nov 12, 2017

    @moos
    Contributor

    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.

  6. gaearon commented on Jan 8, 2018

    @gaearon
    Contributor

    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.

  7. gaearon commented on Jan 9, 2018

    @gaearon
    Contributor

    I merged #3387.

  8. added a commit that references this issue on Jan 9, 2018
    dccc752
  9. gaearon commented on Jan 15, 2018

    @gaearon
    Contributor

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

  10. moos commented on Apr 13, 2018

    @moos
    Contributor

    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"
       }
    
  11. added a commit that references this issue on Jul 10, 2018
    a56cfdd
  12. added a commit that references this issue on Aug 14, 2018
    8dbb7c7
  13. locked and limited conversation to collaborators on Jan 19, 2019
  14. added a commit that references this issue on Oct 8, 2024
    70891b5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions