#Environment variables are always undefined in SSG ( production )

1 messages · Page 1 of 1 (latest)

ebon linden
#

I'm truly lost about how to use environment variables in production with Astro. All NON-Public env vars are always undefined.

i've tried this 3 approaches:

`ts
const {
STRAPI_URL,
} = loadEnv(process.env.NODE_ENV, process.cwd(), "");

console.log({
STRAPI_URL,
})
console.log(import.meta.env.STRAPI_URL)
console.log(process.env.STRAPI_URL)`

I've read the documentation but i still don't understand what i'm doing wrong.

thanks !

fast wharf
#

is this after deploying to your host or does it also happen if you do astro build && astro preview to run a local production build?

#

if it works locally (production build) but not on your host, you need to add those variables using whatever system the host provides

ebon linden
#

Hi ! Since the variables are undefined at build time, the build fail. I've tried with / without Docker.

I've tried with .env file or defining it with the build command like : STRAPI_URL=xxx yarn build

But' it's still undefined 😐

#

I'm hosting on Koyeb.com, and I have the problem too ( i've defined the vars in their system )

fast wharf
#

hmm, I'm not sure, then. Doing a local production build should definitely pull them from the .env file :/

ebon linden
#

Is this mandatory to read from an .env FILE ? How could we know the path of the file in a host like Koyeb or another ? 🤔

fast wharf
#

not mandatory, it's just more convenient while locally developing. Your host should provide similar functionality. I use Netlify and it has an Environment variables section where you add them for the deployment project. Also, I think you can ditch the testing of process.env because Astro uses import.meta.env instead.

#

for Docker....I think you can add them to the yaml for your container or maybe even in the build for the image. It's been a while since I've used Docker.

#

but wherever you're putting those values you don't want to check them in

#

for sensitive info

#

I see this in the Koyeb docs:

If you are using the Dockerfile build process and you wish to access environment variables during the build, you must have ARG instructions in your Dockerfile defining the environment variables you wish to use.

Does this apply to you?

ebon linden
#

Koyeb does have this system too, but i don't really know how it inject the variables. So instead of building again and again on this platform, i'm trying to make it work locally. With / without Docker

#

Yes, i've tried and i cleary see my vars in the dockerfile context

#

`
ARG STRAPI_URL

ENV STRAPI_URL=$STRAPI_URL

RUN STRAPI_URL=${STRAPI_URL} yarn dlx [email protected] run build --filter=web...`

#

I've tried Docker too, but i prefer without if i can. But i jut's can't figure out why it doen't works :/

glossy hamlet
#

Where are you running the code? On the server or on the Client?

ebon linden
#

On the server !

#

I even tried to log the vars explicitly in the front matter to be sure

glossy hamlet
#

So in your frontmatter. If you do

console.log(import.meta.env.STRAPI_URL)

It comes back as Undefined?

ebon linden
#

exactly. Only the PUBLIC_ ones are defined

glossy hamlet
#

I just did this and it works for me

ebon linden
#

Should this work ?

STRAPI_URL=https://xxx yarn run build --filter web...


const {
  STRAPI_URL,
} = loadEnv(process.env.NODE_ENV, process.cwd(), "");

console.log({
  STRAPI_URL,
})```
glossy hamlet
#

Oh you need to access it inside your config?

ebon linden
#

No but i'm trying to see where it work / where it doesnt ahah

#

I don't know if this should work by passing the variable like this, in the build command

#

Okay, i think the problem is the use of turborepo !

glossy hamlet
#

I think you have to do ENV a bit different in that. One sec

ebon linden
glossy hamlet
#

Interesting

ebon linden
#

Yeah, i never had to do this with Strapi, very weird 🤔

#

Well thanks @glossy hamlet and @fast wharf for your time, i truly don't know how this can work with Strapi with the exact same command. I will go with the "--env-mode=loose" flag for now !