Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Environment variables arent accessible in app level? #18

Closed
subhendukundu opened this issue Apr 12, 2021 · 4 comments
Closed

Environment variables arent accessible in app level? #18

subhendukundu opened this issue Apr 12, 2021 · 4 comments

Comments

@subhendukundu
Copy link

I have added a .env in root with

VITEDGE_TEST="hello world"
HELLO_TEST="hello world"

Then trying to access them in src/App.jsx like so

function App ({ router }) {
  console.log(process.env.VITEDGE_TEST, process.env.HELLO_TEST)

but both are undefined. Am I doing something wrong?

@frandiox
Copy link
Owner

frandiox commented Apr 12, 2021

@subhendukundu For the frontend env variables you need to follow Vite instructions: https://vitejs.dev/guide/env-and-mode.html#env-files

only variables prefixed with VITE_ are exposed

-- The VITEDGE_ prefix is for backend variables, which are not loaded by Vite.

@subhendukundu
Copy link
Author

@frandiox
Copy link
Owner

Are you running on mode example? Normally you would use .env or .env.development for the name of your files. Other than that, use import.meta.env.VITE_XXX int he frontend to access variables, that's the recommended way in Vite.

In backend it's still process.env.VITEDGE_XXX since that doesn't run on Vite.

@subhendukundu
Copy link
Author

oh no, I have a .gitignore with the .env, so it wouldn’t push it. People usually push it with .env.example to give an example of how to use the .env. Sorry I forgot to mention that.

But import.meta.env.VITE_XXX did the trick. Didn't know about this one. Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants