Pages/Workers not picking up NextJS .env vars

I'm trying to deploy a NextJS-based Page (+ route handlers which iiuc are deployed as Workers), and it doesn't seem to be picking up my .env vars. It only works if I input them manually into the dashboard. Weird because the build logs always says it pulls in .env. Do I need to use .toml or something?
7 Replies
akazwz
akazwz•4mo ago
yes. you should use wrangler.toml . [vars] MY_VARIABLE = "xxxx" in it and then const myVariable = getRequestContext().env.MY_VARIABLE. you can check https://developers.cloudflare.com/pages/framework-guides/nextjs/deploy-a-nextjs-site/#top-level-getrequestcontext
Cloudflare Docs
Full-stack deployment · Cloudflare Pages docs
Deploy a full-stack Next.js site (recommended).
Dario
Dario•4mo ago
@kyle the logs say that they pull from .env because they pull build time environment variables from there, not runtime ones.... (I know it's super confusing! It's a general wrangler issue I'll look into changing that!) 😓
Dario
Dario•4mo ago
As @akazwz you should put your variables in the toml file, but that only works if you're using the toml file for deploying (i.e. you've set a pages_build_output_dir value (https://developers.cloudflare.com/pages/functions/wrangler-configuration/#configuration-via-wranglertoml)), if you're not, you should set them in the Cloudflare dashboard regardless on the above, secrets need to go in a .dev.vars file for local development and (always) set directly in the Cloudflare dashboard UI for deployments (unless you want to do it via the wrangler pages secrets command)
Cloudflare Docs
Configuration · Cloudflare Pages docs
If your project contains an existing wrangler.toml file that you previously used for local development, make sure you verify that it matches your …
kyle
kyleOP•4mo ago
got it, ty both!! @Dario do you know if it's specifically Workers that don't pull from .env, or is it both Pages and Workers? ie for nextjs does this apply to both pages and route handlers
Dario
Dario•4mo ago
yeah both Pages and Workers don't pull from .env files 😓
kyle
kyleOP•4mo ago
ok that's good to know. I'll just retire the file and use dash only
Dario
Dario•4mo ago
yeah sorry about that, that's the only option for now 🥲 (and use .dev.vars locally if you want)
Want results from more Discord servers?
Add your server