React and Environment Variables

Create-react-app 1.0.0 added support for automatically reading configuration files for the three pre-defined environments: development, test, and production.

As before, default (lowest priority) values may be defined in .env, and the value of NODE_ENV is set automatically based on the script used:

  • start → development
  • test → test
  • build → production

The 1.0.0 release adds built-in support for a configuration file corresponding to each of these: .env.development, .env.test, and .env.production. The values from these files have higher priority than the values in .env. Each file (including the default file) supports another layer of overrides by adding .local to the end.

The full order of precedence is (highest first):

  • shell
  • .env.{environment}.local
  • .env.{environment}
  • .env.local
  • .env

For more information see https://medium.com/@tacomanator/environments-with-create-react-app-7b645312c09d.

Leave a Reply

Your email address will not be published. Required fields are marked *