TanStackT
TanStack3mo ago
4 replies
tricky-teal

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


at this point i'm completely stumped and almost positive i'm just doing something wrong with cloudflare/wrangler
Was this page helpful?