Can't access Environment Variables in Loader via import.meta.env...
#13471
Answered
by
nikolailehbrink
nikolailehbrink
asked this question in
Q&A
Replies: 2 comments 6 replies
-
|
Maybe
|
Beta Was this translation helpful? Give feedback.
5 replies
-
|
Filed an issue for this: #13526 |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi there,
I'm working with React Router v7 as a framework and encountering an issue accessing environment variables server-side.
I have an
.envfile with myOPENAI_API_KEYstored inside.In a loader function, I want to access it via
import.meta.env.OPENAI_API_KEY, but the value is alwaysundefined.When I use
process.env.OPENAI_API_KEYinstead, it works.My expectation was that
import.meta.envshould work server-side, similar to how it behaves in Astro (which also uses Vite):I understand that client-side environment variables must be prefixed with
VITE_, but here I want to stay server-only — I just want to useimport.meta.envinstead ofprocess.env.Question
Why isn't
import.meta.envavailable server-side in React Router v7? Is there a recommended way to configure this or is it expected to stick withprocess.env?Thanks!
Beta Was this translation helpful? Give feedback.
All reactions