React-boilerplate: Ways to deal with environment variables

Created on 6 Jun 2016  路  3Comments  路  Source: react-boilerplate/react-boilerplate

First of all, thank you for your awesome project,
Given our app have interaction with an external api (for example, https://my-api.com/posts)
In favor of http://12factor.net/config, we should start our apps like this:

API_HOST=https://my-api.com/posts npm start:prod

Currently my workaround solution is:

  • Read the API_HOST & manually pass it into webpack like this
new webpack.DefinePlugin({
      'process.env': {
        NODE_ENV: JSON.stringify('production'),
        API_HOST: JSON.stringify(process.env.API_HOST),
      }
    }),
  • Then I store it in config file, or pass it into store

Does we have any better method to deal properly with this issue? about security? DRY?

Most helpful comment

I use a combination of dotenv and https://github.com/ArikMaor/extended-define-webpack-plugin

I usually have a separate place for defining the environment variables I want to expose, and require that inside of the webpack configuration, just so that I don't have to duplicate that stuff or maintain it inside of the webpack config

The extended define makes it cleaner overall to include not only public credentials and hostname values like you do, but other kinds of feature flags which can be toggled on and off through the environment variables

All 3 comments

I use a combination of dotenv and https://github.com/ArikMaor/extended-define-webpack-plugin

I usually have a separate place for defining the environment variables I want to expose, and require that inside of the webpack configuration, just so that I don't have to duplicate that stuff or maintain it inside of the webpack config

The extended define makes it cleaner overall to include not only public credentials and hostname values like you do, but other kinds of feature flags which can be toggled on and off through the environment variables

Thanks for the great answer @datapimp!

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

theterra picture theterra  路  3Comments

zamrq picture zamrq  路  3Comments

chaintng picture chaintng  路  4Comments

fobus42 picture fobus42  路  3Comments

VadimuZz picture VadimuZz  路  4Comments