Skip to content

Instantly share code, notes, and snippets.

@juniormartinxo
Last active September 7, 2021 13:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save juniormartinxo/9c1ab6f0a9bb8e57a091297f2df122a9 to your computer and use it in GitHub Desktop.
Save juniormartinxo/9c1ab6f0a9bb8e57a091297f2df122a9 to your computer and use it in GitHub Desktop.
Utilizando variáveis de ambiente com React

Utilizando variáveis .env com React

  1. Instale o "dotenv"
yarn add dotenv --save
  1. Instale o "dotenv-expand"
yarn add dotenv-expand
  1. Para utilizar
  • Crie o arquivo .env na raíz do projeto e crie uma varíável qualquer. Ela, obrigatoriamente, deve iniciar com "REACT_APP_", exemplo:
REACT_APP_URL_API='http://localhost:3333/api'
  • Importe os arquivos na index.js e crie uma constante para chamar a config() do dotenv, no exemplo nomeamos a constante de "config":
import dotenv from 'dotenv'
import dotenvExpand from 'dotenv-expand'

const config = dotenv.config()

dotenvExpand(config)
  1. Utilizando as variáveis Para utiizar as variáveis criadas no arquivo .env, basta inserir no código da seguinte forma:
process.env.REACT_APP_URL_API
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment