71

16

23

React Environment Variables

Suprabhab Aug 20 ・3 min read

What is ENV πŸ€”

env file to store your sensitive credentials like API keys. Environment variables can also help you to store your API link in one location so that you don't have to change the link in each file manually.

Do you have to install any plugin to use ENV file πŸ™„

No, A React frontend connected to a Node backend is a rock-solid combination for any application you want to build. So you can create and use .env file without installing any plugins. Usually, you create one .env file which triggers for local, integration and production.

What if you want to create different Keys for integration and production

If you have to use different keys for integration and production, in that case you have to customise your environment.
Now, let's see what are we going to cover in this article:

  1. Customise Environment Variable for build environment
  2. Use Environment variable in HTML

1. Customise Environment Variable for build environment

There are already few environment which React supports like .env.development ,.env.test , .env.production , etc.

.env files can be used:

  • .env
  • .env.local:
  • .env.development, .env.test, .env.production : Environment-specific settings

You can create your own env file which is not added in react. You need to install one package called env-cmd for setup the custom environment. Create a file ca

  • reate a file called.env.integration where.env exist
  • .env.local:
  • Set the environment variable which you wanted for integration in .env.integration,
  • REACT_APP_NAME = XXXX
  • install .env.cmd
  • $ yarn add env-cmd
  • Add a new script to your .package.json building with your new environment:
  • { "scripts": { "build: integration": "env-cmd -f .env.integration yarn run build" } } }
  • Now you can run the integration command yarn run build:integration to build with the integration environment config.

πŸ“ Variables in .env.productionwill be used as fallback because NODE_ENV will always be set to production for a build.

What if you want to install devDependencies when NODE_ENV=production πŸ€”

As you know if NODE_ENV=production , then devDependencies packages will not get installed.

However, You can install the devDependencies in NODE_ENV=production with setting yarn install --production=false in package.json .

Checkout this Link for more information.

2. Use ENV variables in HTML

You only use environment variable in JavaScript files. But what when you need to use environment variable in HTML, It can be Google Map keys or any secret keys of script.

You can access the environment variables in the index.html, just to remember that the env variable should be prefix with REACT_APP_.s

title %REACT_APP_NAME% title>

Define permanent environment variables in .env file in root of the project.

.env:

REACT_APP_NAME = ProjectName

πŸ“ Remember to restart the server when you make any changes in .env file.

Reference 🧐

Summary β…€

If you want to add any other env which is not mentioned in react app, you have to setup env-cmd and can use it. Also, you can use .env variables in HTML file using REACT_APP_ Thanks for reading the article ❀️

invitame a un cafe
🌟 Twitter πŸ“š Ebooks 🌟 Instagram
Discussion (3)
expand_more expand_less
R. Maulana Citra
β€’ Aug 22

Thanks for sharing🀩, nice article

2
likes
Reply
expand_more expand_less
Mike From CodeSpectre
β€’ Aug 22

Thanks for sharing

6
likes
Reply
Code of Conduct
β€’
Report abuse
Suprabha
πŸ”₯ New JS, CSS, HTML articles every week
πŸ”₯ Frontend engineer @gojekindonesia πŸ‘©πŸ»β€πŸ’»
http://instagram.com/suprabhasupi
LOCATION
India
WORK
Frontend Engineer
JOINED
25 Jun 2018