Home > Software engineering >  Cannot reach key from .env file
Cannot reach key from .env file

Time:12-01

I'm on React.Js and using emailjs to manage contact form. In order to sends the form, I must fill the user-id and template-id. But I don't want it to be visible so I put it on the .env file like that:

REACT_APP_USER_ID= user_id
REACT_APP_TEMPLATE_ID= template_id

To pass these values easely in the sendForm() property, I've put them on variables : (useContact.jsx)

const template = REACT_APP_TEMPLATE_ID
const user = REACT_APP_USER_ID

But unfortunately with this config the form is not sent.

my folder architecture [.env, package.json, .gitignore, src/Components/Contact/useContact.jsx]

It works when pass the raw version of the values.

Thank You.

CodePudding user response:

You can access the environment variables by accessing process.env variable like this

const template = process.env.REACT_APP_TEMPLATE_ID
const user = process.env.REACT_APP_USER_ID

Note: you have to restart the server after updating .env file

CodePudding user response:

Env variables are accessible through process.env

It depends how are you loading your .env file, I would suggest you to take a look at dotenv package in order to load your env files

An example you could follow it's the one from this stackoverflow answer

  • Related