The doc says: "any vars key are exposed as global variables", but they are not

I want to define something like DEV=true to detect dev mode (followed by some different initiating path), but access variable "DEV" in top level code just cause: "ReferenceError: DEV is not defined". Is this a misunderstanding of the doc? I'm confused, the doc is in https://developers.cloudflare.com/workers/wrangler/environments/#staging-and-production-environments
Cloudflare Docs
Environments · Cloudflare Workers docs
Deploy the same Worker application with different configuration for each environment.
No description
No description
No description
9 Replies
Idle
Idle2w ago
i think that'd be env.ENVIRONMENT
Walshy
Walshy2w ago
indeed ^ will make sure the docs are updated
ch3coohlink
ch3coohlinkOP2w ago
I'm using different key than the example, and the env variable does not exist outside fetch function i see my problem still remains though... seems no eazy way to detect dev mode in the init time tried the "define" option in wrangler, but it didn't substitute the target string in my main.ts. What's the target files this option tries to substitute? info missing in the doc. @Walshy | Workers/Pages
Walshy
Walshy2w ago
best way for you to do this is the env var you were doing before this is how i do it in hono: https://github.com/WalshyDev/blob-builds/blob/main/worker/src/handlers/all.ts#L13-L14 middleware and just set based on env
ch3coohlink
ch3coohlinkOP2w ago
thanks, I'll try
theo
theo2w ago
the "define" options do global replacements using esbuild. It should work, but the format is a little funky. How were you calling it? and also that assumes you are not doing a custom build
ch3coohlink
ch3coohlinkOP2w ago
thanks, i changed the define format in wrangler.toml and it works
ch3coohlink
ch3coohlinkOP2w ago
for users searching for similar issues later, I post the define format here.
No description
ch3coohlink
ch3coohlinkOP2w ago
and the dev command: wrangler dev --define $$IS_DEV$$:true

Did you find this page helpful?