0

I am trying to save my API key as an environment variable, but I don't know how to reach it... I npm installed dotenv, created the file in the root directory, and put the key there. I try to access it in a js file by using process.env.API_KEY but it says that the variable is undefined.

I tried putting require('dotenv').config() at the top of the file I need the key in, but it gives me an error...

ERROR in ./node_modules/dotenv/lib/main.js 2:13-28
Module not found: Error: Can't resolve 'path' in ...

I think I am either putting it in the wrong file, or I am missing something entirely

prools
  • 1
  • 1

1 Answers1

0

As a summary:

To read the API_KEY variable in your react layer, you will need to export the variale REACT_APP_API_KEY before the build and the on any react part, you could get the value with

process.env.REACT_APP_API_KEY

Check the links to understand why the prefix REACT_APP_ is required.

Also if your token is a long live or non non-expirable token, you should not expose it to the frontend (react). Only expirable tokens (authorization code oauth2 grant) should be exposed to the frontend layer.

Check the references to understand how to handle the frontend variables

References

JRichardsz
  • 14,356
  • 6
  • 59
  • 94