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
.env
files during local development (whether you’re usingwrangler dev
directly 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
.env
file, expecting a tool (such as the dotenv package) to load these into an application’s accessible environment during local execution. The primary purpose of.env
is 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
,production
etc) and individual developer preferences get more complex. It’s becoming more common to now commit.env
files 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
.env
files 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
env
object (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.vars
file in the root of your project. This file is loaded when you run a local dev session and any variables are added to theenv
object of your Worker. This.dev.vars
file 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
.env
file. Currently, Wrangler reads.env
files 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'senv
object at runtime. This specific use of.env
files 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
.env
files for configuring your Worker’s runtime environment during local development. This is an alternative approach to the existing.dev.vars
file usage.This means you will have two options for specifying local environment variables:
Using
.env
files: You can define local environment variables in a familiar.env
file, 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.vars
file is detected for your current development context.Using
.dev.vars
files (Existing method): If a.dev.vars
file (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.env
file loading mechanism if desired. In this scenario,.env
files will not be used to populate the Worker'senv
object (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
.env
files(This applies when no relevant
.dev.vars
file is present).env
files..env.local
is loaded first (if it exists)..env
is loaded (if it exists)..env.local
taking precedence over.env
in case of conflicts..env
files prefixed withMINIFLARE_
,WRANGLER_
orCLOUDFLARE_
are filtered out and not added to the Worker’senv
object. They are used to configure Wrangler the tool.vars
from Wrangler configuration are loaded, with the merged variables from.env.local
and.env
taking precedence.Using
.dev.vars
(This applies when a relevant
.dev.vars
file is present and takes precedence)dev.vars
is loaded.env
(or any other `.env.*` files) are not loaded into the Worker’senv
object. They may still be used to configure Wrangler itself if they haveWRANGLER_
orCLOUDFLARE_
prefixes.vars
from Wrangler configuration are loaded, with.dev.vars
taking precedence in case of conflicts.Scenario 2: Running local dev with a specified environment (such as
--env=staging
)Using
.env
files(This applies when no relevant
.dev.vars
or.dev.vars.staging
files are present).env
files in the following order of precedence (the more specific the file, the higher the precedence:.env.staging.local
.env.staging
.env.local
.env
.env
files (.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.vars
from the Wrangler configuration (including those specific to thestaging
environment, if defined) are loaded, with the merged variables from the.env.*
files taking precedence.Using
.dev.vars
or.dev.vars.staging
(This applies when a relevant
.dev.vars
or.dev.vars.staging
file is present and takes precedence).dev.vars.staging
exists, it is loaded. Only values from the most specific file are loaded..dev.vars
exists and no.dev.vars.staging
was found,dev.vars
is loaded..env.*
files are not loaded into the Worker’senv
object.vars
from the Wrangler configuration (including those specific to thestaging
environment, if defined) are loaded, with the variables from the active.dev.vars
file (ex:.dev.vars.staging
or.dev.vars
) taking precedence.Notes on merging and precedence for
.env
files.env
files override less specific ones. Environment-specific files override general ones..env.staging.local
->.env.staging
->.env.local
->.env
.env
files and.dev.vars
files are loaded differently..env
files: Following common practice, variables will be merged from the hierarchy of.env
files..dev.vars
files: To maintain existing behavior, only the single, most specific.dev.vars
file (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’senv
object. 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'senv
object, regardless of the file they originate from (.env
or.dev.vars
).dev.vars
files, doing so would be a breaking change for existing.dev.vars
users, so this filtering currently applies when.env
files are the source for Worker runtime variables.Variable expansion
We will also support variable expansion in
.env
files. This means you can reference other variables defined with the same file or previously loaded.env
files (respecting the precedence order). For example:Variable expansion is only available for
.env
files and is not supported for.dev.vars
files. Additionally, variable expansion does not extend to thevars
defined 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
.env
file containingBASE_URL
.We do not plan to support command substitution.
Integration with the Cloudflare Vite Plugin
The proposed
.env
file handling will extend to projects using the Cloudflare Vite plugin, ensuring a consistent experience. Here’s how it will generally work:CLOUDFLARE_ENV
environment variable to indicate which “Cloudflare environment” to use. This can be defined within your.env
files (for example, in.env.staging
you 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..env
files (.env
,.env.staging
, etc) to populateimport.meta.env
for build-time replacements and for its own configuration.env
object. If no.dev.vars
file is present for the target Cloudflare environment, the plugin will use the relevant, merged.env
files (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.env
files according to its mode (typically "production") for build-time operations (like replacingimport.meta.env.VITE_XXX
). Runtime variables for your Worker declared in.env
files during local development cannot be accessed via the Worker’senv
in production. Deployed Workers will continue to source their runtime variables from your Wrangler configuration and secrets uploaded viawrangler secret put
or the Cloudflare dashboard. This ensures that local development conveniences from.env
files do not inadvertently become the source of truth for production runtime configuration.Considerations:
wrangler types
command currently generates types forvars
bindings from the values in.dev.vars
when generating theEnv
interface. This would now include any non-prefixed variables found in the merged.env
(and.env.<environment>
files, which may not be expected / make theEnv
type more verbose..env
files 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