Can't access env vars in Workers build

I'm migrating some Astro projects, that build static pages, from Cloudflare Pages to Cloudflare Workers. I'm following the migration tutotial and looking the docs. But I 'cant access the environment variables defined in wrangler.toml in process.env during or after the build. I using the compat flags: compatibility_flags = [ "nodejs_compat", "nodejs_compat_populate_process_env" ] But in any moment i'm seeing the variables in log Is there anything else i need to do?
10 Replies
Hard@Work
Hard@Work2w ago
Have you added those variables to Settings > Build > Build Variables and Secrets? Or do you mean they aren't present once deployed
Santho
SanthoOP2w ago
I added some secrets in the panel, in this path you mentioned. But i have others vars that are defined in wrangler.toml The secrets from panel appear in process.env, but the vars from wrangler.toml dont
Hard@Work
Hard@Work2w ago
To confirm, you are logging process.env during the build right, not against your deployed Worker?
Santho
SanthoOP2w ago
Yes. I'm lookin in the Build history log
Hard@Work
Hard@Work2w ago
Yeah, that's expected. The variables in your wrangler.toml/jsonc are the variables present on your deployed Worker. They wouldn't be available in the build environment
Santho
SanthoOP2w ago
Ohh. That's a bummer In build time the only env vars present is the ones defined via panel?
Hard@Work
Hard@Work2w ago
Yes, that's correct For example, if you have a build variable configuring the version of Node you need to build your project, it wouldn't make sense to add that to the deployed Worker, and vice versa
Santho
SanthoOP2w ago
Thats makes sense. Is there another way to define build vars for workers, or only in the panel?
Hard@Work
Hard@Work2w ago
I think it is only via the panel right now? Maybe the API?
Santho
SanthoOP2w ago
Interesting. I will check this option Thanks a lot!

Did you find this page helpful?