on my local dev machine (linux), import.meta.env.SOME_VAR works fine, but when i build (the project and a docker image) and deploy to my server also running linux, import.meta.env doesnt work. i mean this for auth.config.ts for the auth lib. also it doesnt automatically recognize AUTH_SECRET env var even when the secret field is left empty. also same thing inside .astro files.
#environment variables not working
9 messages · Page 1 of 1 (latest)
like i get this error cause import.meta.env.MONGO_URI is undefined, but its not. (also funnily enough it worked perfectly fine a couple days ago)
if you want to try replicating, here is the source https://gitea.overflow.fun/koneko/dd-multitool
under the "web" folder. have a look at the docker-compose.yml as to what you're supposed to pass as env vars
Are the variables defined in your server environment? I remember deploying my Astro SSR site and the build keeps failing because apparently the vars are not defined
I didn't get around to automating it, but what I did was I defined them manually like SOME_VAR=VALUE
yes, in the container which runs the built image, it has all the env vars (i checked manually)
I'm not an environment variables expert, but I think when defining the values in your compose file, those are runtime variables, and this works the same as doing something like:
SOME_VAR=... node ./dist/server/entry.mjs
So, instead of import.meta.env.SOME_VAR, you might need to use process.env.SOME_VAR here.
process.env.SOME_VAR works for example in auth.config.js. however process.env.SOME_VAR does not work in an endpoint or lib or something that astro uses. (see https://gitea.overflow.fun/koneko/dd-multitool/src/branch/main/web/src/lib/db.ts with it crashing https://gitea.overflow.fun/koneko/dd-multitool/src/branch/main/web/src/pages/planner/create.astro after it calls connectToDatabase())
Hmm, this is odd. process.env should work in endpoints too. I'm using this with nodemailer and Docker.
Maybe in your case this is not working because of the fallback const mongoUsername = import.meta.env.MONGO_USERNAME || process.env.MONGO_USERNAME; and for some reason it doesn't hit || process.env.MONGO_USERNAME?