How to get variable from .env file?

50 Views Asked by At

I'm trying to get my API Key from the .env file in my project using JS, but any way to import that I have tried so far hasn't worked. So far, I've tried

.env: API_KEY="abc123" file: const key = process.env.API_KEY;

.env: REACT_APP_API_KEY="abc123" file: const key = process.env.REACT_APP_API_KEY;

.env: REACT_APP_API_KEY="abc123" file: const key = import.meta.env.REACT_APP_API_KEY;

.env: VITE_REACT_APP_API_KEY="abc123" file: const key = import.meta.env.VITE_REACT_APP_API_KEY;

and nothing has worked. I'm importing the key directly into an html file inside a script tag, and I'm not sure if that would have an effect on the import or not. Hard-coding the key into the html file works fine; it only gets wonky on import. Is there anything else I can try?

0

There are 0 best solutions below