Create-react-app: Feature request - Support different env configs

Created on 3 Jan 2017  路  11Comments  路  Source: facebook/create-react-app

Read different .env configs according to current command (start / test / build).

  • Read .env.dev when npm start and npm test
  • Read .env.prod when npm run build

By default (if custom config does not exist) read env variables from .env file.

dotenv is using for config parsing.

Not sure about npm test - what config file should be accepted.

proposal

Most helpful comment

Provide simple PR with feature implementation

All 11 comments

But according to dotenv FAQ

Should I commit my .env file?

No. We strongly recommend against committing your .env file to version control. It should only include environment-specific values such as database passwords or API keys. Your production database should have a different password than your development database.

Should I have multiple .env files?

No. We strongly recommend against having a "main" .env file and an "environment" .env file like .env.test. Your config should vary between deploys, and you should not be sharing values between environments.

Provide simple PR with feature implementation

馃憤

Nice improvement. It would be very useful!

@tuchk4 , I want to know: How can i use the variables which from .env file in my ES6 code?

@dioxide

These environment variables will be defined for you on process.env. For example, having an environment variable named REACT_APP_SECRET_CODE will be exposed in your JS as process.env.REACT_APP_SECRET_CODE, in addition to process.env.NODE_ENV.

Update issues according to latest PR updates

What .env* files are used?

  • .env - Default
  • .env.development, .env.test, .env.production - Environment-specific settings.
  • .env.local - Local overrides. This file is loaded for all environments except test.
  • .env.development.local, .env.test.local, .env.production.local - Local overrides of environment-specific settings.

Files priority (file is skipped if does not exist):

  • npm test - .env.test.local, env.test, .env.local, .env
  • npm run build - .env.production.local, env.production, .env.local, .env
  • npm start - .env.development.local, env.development, .env.local, .env

Priority from left to right.

Can you confirm that once this feature is built-in into create-react-app we won't need https://www.npmjs.com/package/react-app-env anymore ?

@cadichris Yes. I will mark that package as deprecated

Please help beta test the new version that includes this change!
https://github.com/facebookincubator/create-react-app/issues/2172

Was this page helpful?
0 / 5 - 0 ratings