T
TanStack•2mo ago
like-gold

Missing environment variables on Cloudflare workers

https://github.com/kylekz/tss-cf-workers i've taken the pnpm create @tanstack/start@latest starter, picked tailwind + eslint + t3env + upgraded all packages to latest. then added one server var and one client var the main branch uses a singular env.ts as per the starter, which doesn't work when running pnpm dev as server vars don't get loaded into import.meta.env, let alone attempting to deploy this the split-env branch has separate client and server env objects: client using import.meta.env and server using process.env (which should work due to the wrangler compatibility date) in the server function thats called in the index route loader, the server variable access errors due to variables being invalid, so the worker crashes with a 500. i switched this to lazy load the env object, catch the error and return a fallback. then the client object fails validation and errors. i've defined both vars in the worker dashboard under the runtime variables and secrets section, not the build section. i've also tried importing env from cloudflare:workers which results in the same issue. in my actual app, i can't even finish a deploy as i assume the build/deploy process attempts a health check of some sort, but hits the invalid environment variables error and the deploy fails:
✘ [ERROR] A request to the Cloudflare API (/accounts/.../workers/scripts/my-app) failed.

Uncaught Error: Invalid environment variables
at null.<anonymous> (assets/worker-entry-D6oWwPth.mjs:40779:11)
at null.<anonymous> (assets/worker-entry-D6oWwPth.mjs:40784:29) in createEnv
at null.<anonymous> (assets/worker-entry-D6oWwPth.mjs:40807:15)
[code: 10021]
To learn more about this error, visit:
https://developers.cloudflare.com/workers/observability/errors/#validation-errors-10021
✘ [ERROR] A request to the Cloudflare API (/accounts/.../workers/scripts/my-app) failed.

Uncaught Error: Invalid environment variables
at null.<anonymous> (assets/worker-entry-D6oWwPth.mjs:40779:11)
at null.<anonymous> (assets/worker-entry-D6oWwPth.mjs:40784:29) in createEnv
at null.<anonymous> (assets/worker-entry-D6oWwPth.mjs:40807:15)
[code: 10021]
To learn more about this error, visit:
https://developers.cloudflare.com/workers/observability/errors/#validation-errors-10021
at this point i'm completely stumped and almost positive i'm just doing something wrong with cloudflare/wrangler
4 Replies
genetic-orange
genetic-orange•5w ago
Hey did u managed to resolve this issue ?
like-gold
like-goldOP•5w ago
nope
vicious-gold
vicious-gold•2w ago
i've got a project stuck here too - deploying to cloudflare workers and can't access the AI binding either locally or deployed to cloudflare. windsurf has tried 10 different approaches with claude 4.5 and i've asked grok for advice. last answer was switch to Remix which i'd rather avoid ... hoping there is an answer or a quick patch šŸ™‚
stormy-gold
stormy-gold•5d ago
Did you set your CLIENT ENV in the BUILD SETTINGS of the Cloudflare project?

Did you find this page helpful?