What would be the best way to get .env variables into both NodeJS and Vite? #15064
Unanswered
MarByteBeep
asked this question in
Q&A
Replies: 2 comments
-
I also have such problems
when I try to write the following code if (import.meta.env.SSR) {
} |
Beta Was this translation helpful? Give feedback.
0 replies
-
@MarByteBeep have you found a solution? |
Beta Was this translation helpful? Give feedback.
0 replies
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.
-
I'm writing shared code that runs on a server as a NodeJS application (e.g.,
node app.js
) and on a SolidJS server that uses Vite. Both server and NodeJS application use sensitive variables that I would like to store in an.env
file. I mean, that's where these things are for, right?Vite uses
import.meta.env
to access environment variables prefixed withVITE_
. The NodeJS application can usedotenv
, however that will put those environment variables inprocess.env
.Ideally I'd like to write code without having to think about whether I run as a NodeJS app, or a Vite server app. I'm okay with prefixing all variables with
VITE_
for the NodeJS application as well. However I don't know how to properly switch betweenprocess.env
andimport.meta.env
.Currently I came up with this work-around:
Now my NodeJS application will start with
import 'dotenv/config';
and the Vite server application will work out of the box. This works, but I don't particularly like that assumption onimport.meta?.env
being undefined in NodeJS applications, nor do I like to have to declare thatline each and everytime I use shared environment variables.
Anyone with a better idea?
Beta Was this translation helpful? Give feedback.
All reactions