Replies: 8 comments 19 replies
-
|
In my opinion Workers should inject current shell environment variables into the worker, this way you can use your own approach to add env variables like Doppler, Infiscal or .env files via the dotenv-cli. This is also the most obvious way to add env variables from a user perspective, simply add env variables to the shell, like you do in Node or any other runtime. Right now i use doppler to manage env variables and i have to run a script to download the env variables to a .dev.env file, if you added support for .env i would need to do the same but download them to an .env file. What i would like to do instead is for workers to use my shell environment so i can just use the doppler run command to run wrangler with my secrets. Another possible solution would be to have a default worker environment called development and inject secrets from that env into the worker. This way the user would use the Cloudflare dashboard to add env variables. |
Beta Was this translation helpful? Give feedback.
-
|
Thanks everyone for your feedback -- both publicly and privately! Some updates here:
Will follow-up about timelines shortly. |
Beta Was this translation helpful? Give feedback.
-
|
Very excited about this. My only real concern is with "Any variables from the merged .env files prefixed with MINIFLARE_, WRANGLER_ or CLOUDFLARE_ are filtered out and not added to the Worker’s env object. They are used to configure Wrangler the tool.". Technically I completely understand this with wrangler/mf using |
Beta Was this translation helpful? Give feedback.
-
|
It would also be useful to have a command line arg ( |
Beta Was this translation helpful? Give feedback.
-
|
Following along with the implementation here: #9914 |
Beta Was this translation helpful? Give feedback.
-
|
A new feature request that has just surfaced is the ability/requirement(?) to lock down which values are read from Advantages
Disadvantages
Feedback We are close to landing this |
Beta Was this translation helpful? Give feedback.
-
|
Thank you to everyone for your input to this feature. |
Beta Was this translation helpful? Give feedback.
-
|
I am currious about the structure here. This sounded like a great feature but it seems perhaps I misunderstood the problem it aimed to solve. I have about 15-20 different workers, many of them share the same environment variables and packages so I have a workers repo that contains them all within a single package.json file. My assumption was that dropping the .env file at the root of the project as the docs define would allow me to share the same env variables with all the workers in that package. It seems perhaps thats not the case as it only works if the .env file is in the same folder as the wrangler.yaml file |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Standardize .env file support for Cloudflare Workers during local development
Hi 👋
Our team is focused on improving the local development experience on Cloudflare Workers, and we’d love your feedback on the following proposal. This proposal outlines plans to support loading environment variables via
.envfiles during local development (whether you’re usingwrangler devdirectly or through integrations like the Cloudflare Vite plugin), allowing you to configure environment variables and secrets in familiar ways.Background
Many developers are accustomed to configuring environment variables and secrets in a
.envfile, expecting a tool (such as the dotenv package) to load these into an application’s accessible environment during local execution. The primary purpose of.envis to keep configuration separate from application code, a principle promoted by the third factor “Config” in the Twelve-Factor App paradigm.As projects grow, managing configuration for different environments (
dev,staging,productionetc) and individual developer preferences get more complex. It’s becoming more common to now commit.envfiles with shared configuration settings across a given project, and use specific file naming conventions for a layered approach. For example:env.local: This file is intended for local development overrides/secrets that an individual developer can set, and should not be committed to version control.Environment-specific files (
.env.staging,.env.production): These files are used to define shared, non-sensitive default configurations for specific environments.In short, many ecosystems and libraries have standardized on how
.envfiles are loaded, and many developers start projects on Cloudflare Workers expecting similar file handling. However, environment variables and secrets are configured differently for Workers applications – making the platform feel less straightforward and resulting in requests like this one. We’ll spend a moment explaining why things are different on Workers, and then lay out our proposal on how to meet developers where they are.Why it’s different on Workers
Cloudflare Workers run in a different runtime (
workerd) that is separate from the tooling environment that Wrangler relies on (Node.js). Because of this, we currently support two different ways to set environment variables at runtime or at buildtime.Runtime variables: To make environment variables available to a Worker’s
envobject (its environment bindings), you can set project-level environment variables as vars in Wrangler configuration (which is checked into version control). For runtime secrets or variables that are strictly for local development and should not be committed to version control (similar toenv.local), Workers supports a.dev.varsfile in the root of your project. This file is loaded when you run a local dev session and any variables are added to theenvobject of your Worker. This.dev.varsfile is today’s primary (and Cloudflare-specific) method for injecting these local-only values directly into your Worker’s runtime context.Buildtime variables: To configure the build process and tools with environment variables (such as Wrangler itself and Node.js), you can set system environment variables in a
.envfile. Currently, Wrangler reads.envfiles and merges their contents with the process environment variables for this purpose only. These variables are used to modify how Wrangler the tool works (ex:WRANGLER_LOG="debug") and are not passed to your Worker'senvobject at runtime. This specific use of.envfiles has confused some users who expect variables defined there to also be available to their Worker code at runtime.Proposal
To better meet expectations, align with ecosystem standards, and provide a more intuitive experience, we propose introducing support for
.envfiles for configuring your Worker’s runtime environment during local development. This is an alternative approach to the existing.dev.varsfile usage.This means you will have two options for specifying local environment variables:
Using
.envfiles: You can define local environment variables in a familiar.envfile, including environment specific files (such asenv.staging). You’ll benefit from local overrides and variable expansion, whether you’re using Wrangler directly or the Cloudflare Vite plugin. This method will be active if no relevant.dev.varsfile is detected for your current development context.Using
.dev.varsfiles (Existing method): If a.dev.varsfile (or its environment-specific counterpart like.dev.vars.staging) is present for your current development context, the system will exclusively use it to load variables into your Worker’s runtime context. This ensures backwards compatibility for existing projects, and provides a clear way to opt-out of the.envfile loading mechanism if desired. In this scenario,.envfiles will not be used to populate the Worker'senvobject (though they may still configure Wrangler itself via specially prefixed variables likeWRANGLER_LOG).File loading and precedence
The source and priority of your Worker's runtime variables during local development depend on which files are present:
Scenario 1: Running local dev with no specified `
--env`Using
.envfiles(This applies when no relevant
.dev.varsfile is present).envfiles..env.localis loaded first (if it exists)..envis loaded (if it exists)..env.localtaking precedence over.envin case of conflicts..envfiles prefixed withMINIFLARE_,WRANGLER_orCLOUDFLARE_are filtered out and not added to the Worker’senvobject. They are used to configure Wrangler the tool.varsfrom Wrangler configuration are loaded, with the merged variables from.env.localand.envtaking precedence.Using
.dev.vars(This applies when a relevant
.dev.varsfile is present and takes precedence)dev.varsis loaded.env(or any other `.env.*` files) are not loaded into the Worker’senvobject. They may still be used to configure Wrangler itself if they haveWRANGLER_orCLOUDFLARE_prefixes.varsfrom Wrangler configuration are loaded, with.dev.varstaking precedence in case of conflicts.Scenario 2: Running local dev with a specified environment (such as
--env=staging)Using
.envfiles(This applies when no relevant
.dev.varsor.dev.vars.stagingfiles are present).envfiles in the following order of precedence (the more specific the file, the higher the precedence:.env.staging.local.env.staging.env.local.env.envfiles (.env.staging.local,.env.staging,.env.local,.env) are merged into a single collection, respecting the precedence order where more specific files override less specific ones.MINIFLARE_,WRANGLER_orCLOUDFLARE_are filtered out.varsfrom the Wrangler configuration (including those specific to thestagingenvironment, if defined) are loaded, with the merged variables from the.env.*files taking precedence.Using
.dev.varsor.dev.vars.staging(This applies when a relevant
.dev.varsor.dev.vars.stagingfile is present and takes precedence).dev.vars.stagingexists, it is loaded. Only values from the most specific file are loaded..dev.varsexists and no.dev.vars.stagingwas found,dev.varsis loaded..env.*files are not loaded into the Worker’senvobject.varsfrom the Wrangler configuration (including those specific to thestagingenvironment, if defined) are loaded, with the variables from the active.dev.varsfile (ex:.dev.vars.stagingor.dev.vars) taking precedence.Notes on merging and precedence for
.envfiles.envfiles override less specific ones. Environment-specific files override general ones..env.staging.local->.env.staging->.env.local->.env.envfiles and.dev.varsfiles are loaded differently..envfiles: Following common practice, variables will be merged from the hierarchy of.envfiles..dev.varsfiles: To maintain existing behavior, only the single, most specific.dev.varsfile (such as.dev.vars.staging) will take precedence and be used exclusively (no variable merging).process.env.NODE_DEBUG) will not be added to the Worker’senvobject. This includes variables added to shell commands such asNODE_DEBUG=foo wrangler dev.MINIFLARE_, WRANGLER_andCLOUDFLARE_prefixed variables are strictly for configuring Wrangler and will not be passed to the Worker'senvobject, regardless of the file they originate from (.envor.dev.vars).dev.varsfiles, doing so would be a breaking change for existing.dev.varsusers, so this filtering currently applies when.envfiles are the source for Worker runtime variables.Variable expansion
We will also support variable expansion in
.envfiles. This means you can reference other variables defined with the same file or previously loaded.envfiles (respecting the precedence order). For example:Variable expansion is only available for
.envfiles and is not supported for.dev.varsfiles. Additionally, variable expansion does not extend to thevarsdefined in Wrangler configuration. For example, if you have the following in awrangler.json:This will be used verbatim and not expanded, even if there is a
.envfile containingBASE_URL.We do not plan to support command substitution.
Integration with the Cloudflare Vite Plugin
The proposed
.envfile handling will extend to projects using the Cloudflare Vite plugin, ensuring a consistent experience. Here’s how it will generally work:CLOUDFLARE_ENVenvironment variable to indicate which “Cloudflare environment” to use. This can be defined within your.envfiles (for example, in.env.stagingyou might setCLOUDFLARE_ENV=staging) to bridge Vite's concept of "modes" with Cloudflare Workers environments. This part of the Vite plugin's behavior will remain..envfiles (.env,.env.staging, etc) to populateimport.meta.envfor build-time replacements and for its own configuration.envobject. If no.dev.varsfile is present for the target Cloudflare environment, the plugin will use the relevant, merged.envfiles (respecting the full precedence order ex:.env.staging,.env) as the source for your Worker's runtime variables, filtering outMINIFLARE_,CLOUDFLARE_andWRANGLER_prefixes. These variables will also support expansion.vite build): When you build your project for deployment, Vite will load.envfiles according to its mode (typically "production") for build-time operations (like replacingimport.meta.env.VITE_XXX). Runtime variables for your Worker declared in.envfiles during local development cannot be accessed via the Worker’senvin production. Deployed Workers will continue to source their runtime variables from your Wrangler configuration and secrets uploaded viawrangler secret putor the Cloudflare dashboard. This ensures that local development conveniences from.envfiles do not inadvertently become the source of truth for production runtime configuration.Considerations:
wrangler typescommand currently generates types forvarsbindings from the values in.dev.varswhen generating theEnvinterface. This would now include any non-prefixed variables found in the merged.env(and.env.<environment>files, which may not be expected / make theEnvtype more verbose..envfiles will be handled in monorepo setups, and will follow-up on this proposal.Call to action
Beta Was this translation helpful? Give feedback.
All reactions