#[Solved]Environment variable won't work outside dev

51 messages · Page 1 of 1 (latest)

merry flume
#

Hello, this is starting to drive me crazy, is there something I don't understand?

I have environment variables, in vite dev I can access them normally via process.env
When I build (nitroV2 with the AWS amplify preset) and start the app (locally or deployed) the variables are undefined.

Locally, they are set in the .env file, and they are formatted like ADMIN_COOKIE="xxx"

It's secret variables used in server context, i've tried multiple way, each of them works in dev mode

  • Getting them outside my serverFN in the same ts file
  • Getting them inside each function
  • Using node:process (and process solo)
  • Creating a ServerOnlyFn to get them, and using this func on my serverFn

the console.log inside each func (serverOnlyfn>serverFN>clientUsage) show the variable in dev mode at each points, but not in builded env.
On aws i set these variables, they are displayed in undefined too.
I have exhausted all my ideas at this point

outer dew
pastel imp
merry flume
#

import.meta.env isn't for client sided variables ? I use it for my VITE_ environment variable

#

(i have to redo it wit createIsomorphicFn() but i used it for shared/public vars, using process.env or import.meta.env to get my publics vars)

pastel imp
#

@merry flume have you tried loading the env explicitely in the vite config ?

outer dew
pastel imp
# outer dew that's what i said isn't it ?

no, you said import.meta.env is for runtime env vars
import.meta.env is for bundle time env vars (which happen to be available for the client, but not exclusive to it)

merry flume
#

i didn't knew that i'll test it

outer dew
#

actually i had the same issue env not showing in productions build and then i used t3-oss package and it fixed it.

pastel imp
#

i get your confusion for import.meta.env being runtime
calling that property "runtimeEnv" was probably a bad idea

#

they should have called it something like envSource

#

fwiw, @merry flume i also use t3-env

outer dew
pastel imp
merry flume
# pastel imp

Do I actually need to do something specific with the const?
i have very light knowledge on vite config, i just saw the define param to explicitely define a variable it is that ?

merry flume
pastel imp
#

no, i just screenshotted a project where i do use the env, but you don't need to

#

otherwise i have no idea other than maybe there is a special place where to add the env vars on aws that i dont know of

outer dew
merry flume
pastel imp
#

i used to have to do this with ts start, and checked that the client output didnt contain my server side vars

#

anyway: server side vars are not replaced at bundle time since those are runtime vars

outer dew
# pastel imp i don't think so

yes that's the case brother case tanstack is isomorphic bydefault if you use any function which load the env it will be on client to.

merry flume
#

even in my local build the environment var are undefined that's what i don't understand, if it's was only on aws...

pastel imp
#

runtime vars cannot be on the client whatsoever

outer dew
pastel imp
#

even if you tried to do something like process.env.MY_VAR on the client, it would be undefined

#

since the client is static, nothing can come from the runtime

#

so it's impossible to leak those

#

the only thing that can leak is the name of the env var

#

not its value

outer dew
#

then sub sequent navigation are on client side.

pastel imp
#

yes, but this happen on prerender/ssr

#

not at bundle time

#

prerender/ssr is runtime, not bundle time

outer dew
#

but it think @merry flume is talking about productions.

pastel imp
#

found this

#

I think Daniel's solution should do the trick

#

they're even using the same setup as you

outer dew
merry flume
#

t3 env do exactly what i planned to implement to handle my env var btw, thx for the discovery

merry flume
#

thx

#

[Solved]Environment variable won't work outside dev