T
TanStack4mo ago
other-emerald

How to use updated client env variable value in production app?

I'm using t3 env + trpc for current tanstack app. After build the image for the bun preset. when i run the app in production i'm getting env variable error as shown in images. I'm running app using bun run .output/server/index.mjs in production. How can i pass env variable when i run the bun run .output/server/index.mjs command and how to actually parse it in trpc routes. Since it server using import.meta.env.VITE_API_URL it not a best option right!!!
No description
No description
No description
No description
8 Replies
like-gold
like-gold4mo ago
You'll need to create two separate files for the env, one for client and one for server, i had the same issue but this worked for me
No description
like-gold
like-gold4mo ago
for the clien.ts the runtime will be: runtimeEnv: import.meta.env
other-emerald
other-emeraldOP4mo ago
Thnx. That actually fixed the issue. One more question. Do client env variable get hard codded after the build? i have the the client env variable and build the app with that env variable value. But when i run the production app with the same client env variable but with new value it not using the updated valuse. Still use the same old valuse that used when building app.
rare-sapphire
rare-sapphire4mo ago
yeah client variables are baked at build time. for runtime client settings the easiest is something like a serverFn that you call in _root to fetch them from the server
other-emerald
other-emeraldOP4mo ago
i'm using t3 env so i don't think that can work!!!
sensitive-blue
sensitive-blue4mo ago
Or you can do something like this, keeping all in the same file
No description
other-emerald
other-emeraldOP4mo ago
That issue is fixed but my consent is how to use updated value in client env variable in production app?
sensitive-blue
sensitive-blue4mo ago
Something like:
SERVER_URL=google.com
VITE_SERVER_URL=$SERVER_URL
SERVER_URL=google.com
VITE_SERVER_URL=$SERVER_URL

Did you find this page helpful?