0

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>
2
  • Not sure if that is the problem, but I've never seen .envrc files in a Node project before, only .env ones. Maybe try to change the name ? Commented Oct 28, 2019 at 19:04
  • Thanks but doesn`t work Commented Oct 28, 2019 at 19:06

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

6 Comments

sorry but how can i do that?
Updated the answer. please check it out.
ok, but i get ERROR in ./node_modules/dotenv/lib/main.js Module not found: Error: Can't resolve 'fs' in '/Users/r13/dev/projects/educat-flask/node_modules/dotenv/lib'
Are you using it in the client side or the server side? what environment is it NodeJS?
client side, Node.js v12.11.1.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.