GustavoNogueira
GustavoNogueira

Reputation: 399

I can't access my environment variable in file.js

I define REACT_APP_ADMIN_URL in my .envrc file, i want to use it as a component link, but i get only empty or undefined

This my environment variable, in file .envrc

REACT_APP_ADMIN_URL="http://127.0.0.1:8000/admin"

in file consts.js i make

export const ADMIN_URL = process.env.REACT_APP_ADMIN_URL;

and in my page i make this

import { ADMIN_URL } from './Consts';

<Menu href={ADMIN_URL}>Admin</Menu>

but doesnt work, in my inspector console i get this

<a href="#" role="menuitem" tabindex="-1">Admin</a>

Upvotes: 0

Views: 1938

Answers (1)

MistaOS
MistaOS

Reputation: 255

I think that you have to load the environment variables from the .env file on runtime, you can use dotenv package to do so.

  1. Install the package using npm i dotenv
  2. Create a .env file in the root directory of your project. Add environment-specific variables on new lines in the form of NAME=VALUE.
  3. Put this line require('dotenv').config() before using the environment variables on your code.
  4. process.env now has the keys and values you defined in your .env file

Upvotes: 1

Related Questions