PazzeG
PazzeG

Reputation: 137

Cannot reach key from .env file

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.

Upvotes: 0

Views: 1196

Answers (2)

Francesco Vattiato
Francesco Vattiato

Reputation: 488

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

Upvotes: 1

Muhammad Huzaifa
Muhammad Huzaifa

Reputation: 276

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

Upvotes: 4

Related Questions