Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

94
Views
REACT_APP_KEY from .env in root not being accessed

I am trying to put my API key in my .env file that is located in the root of my project( on the same level as the package json file)

When I console log the REACT_API_KEY it is read as undefined

I have restarted the server multiple times, everything is saved.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

I recommend using a library called dotenv. This will read your .env file.

let's say you have an environment variable: REACT_API_KEY=APIKEY.

You can access the value like this:

require('dotenv').config();
console.log(process.env.REACT_API_KEY);

This will log: APIKEY

This will be on the backend side, you then need to use some form of node.js library to serve the data to the frontend. Here is an example using express:

const express = require('express');
const app = express();
require('dotenv').config();


app.get('/apikey', (req, res) => {
  res.send(process.env.REACT_API_KEY);
})

Now in the frontend you will need to create a GET request to this uri to actually get the data. Here is an example using axios:

axios.get('/apikey').then((res) => {
  console.log(res);
  // res will be your API key in a String format
})

Obviously, because this key is hidden in the backend, you won't want users to be able to access this endpoint in your API. you can configure which endpoints you want your users to be able to access using a webserver like nginx.

This may seem like a lot for an environment variable, although these steps will solve many of your problems in the future, for things such as user logins and registry, and other data.

This is how you do it without the ReactJS build process. If you are using create-react-app to build your app, you can use React environment variables that are embedded during the build process. Check this out in the react documentation

about 4 years ago · Juan Pablo Isaza Report

0

You need to append process.env at the beginning.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!