Is it possible to use dotenv in a react project?

Sorry for picking up old question, but
react-scripts actually uses dotenv library under the hood.

With [email protected] and higher, you can work with environment variables this way:

  1. create .env file in the root of the project
  2. set environment variables starting with REACT_APP_ there
  3. access it by process.env.REACT_APP_… in components

.env

REACT_APP_BASE_URL=http://localhost:3000

App.js

const BASE_URL = process.env.REACT_APP_BASE_URL;

See docs for more details.

Leave a Comment