🚀 Feature Request: Wrangler to load `.env` files as `.dev.vars`
Describe the solution
Wrangler loads environment variables from .dev.vars as described in the documentation. However, many developers will be familiar with using .env files to manage variables locally. Developers may already have .env files in the repository for other software; in this case, some variables may need to be duplicated and kept up to date in both files.
Related:
- #1468 - Similar, although perhaps
.envshould be automatically searched for if.dev.varsis not found. - #2865, #2992 - Users not understanding the current behaviour
- #818 - Discussion about runtime/buildtime variables
- #1305?
- #190
Thanks for raising this! Unfortunately, Wrangler uses .env for environment variables that configure Wrangler (not your Worker), and so we wouldn't be able to make this change (see https://developers.cloudflare.com/workers/wrangler/system-environment-variables/ for documentation on which environment variables Wrangler supports).
so we wouldn't be able to make this change
This sounds a bit strange to me... A lot of people in at least seven topics vote for using .env at a project level just to be told that it is impossible to implement, but moving wrangler's own settings to something like .wrangler.env does not look that difficult.
I appreciate it is a breaking change and the Team obviously has more important issues to deal with, so it's more like unwillingness, not "wouldn't be able"...
I have to use a workaround so I don't forget to accidentally duplicate variables. I hope it will help someone:
- Create a
copy_env.shfile with the contents:
#!/bin/bash
# Check if the .env file exists
if [ -f ".env" ]; then
# Copy the contents of .env to .dev.vars
# If .dev.vars exists, it will be overwritten
cp ".env" ".dev.vars"
echo "File .dev.vars created or updated with contents from .env"
else
echo "File .env not found"
fi
- Before running the local server, run
sh copy_env.sh && <your command to run server>.
I also have a workaround. I wrote a simple C script to load my .env file to wrangler dev
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
// This script is NOT memory safe
// It's NOT supposed to be used in production!!!
int main() {
// Open file
FILE *fptr;
fptr = fopen("./internal/cloudflare/.env.local", "r");
// get size
fseek(fptr, 0, SEEK_END);
long size = ftell(fptr);
fseek(fptr, 0, SEEK_SET);
// Store the content of the file
char* myString = malloc(size);
// final output
char* finalArgv[128];
memset(finalArgv, 0, sizeof finalArgv);
finalArgv[0] = "bunx";
finalArgv[1] = "wrangler";
finalArgv[2] = "dev";
finalArgv[3] = "--port";
finalArgv[4] = "7777";
int finalOffset = 5;
// Read the content and store it inside myString
fread(myString, 1, size, fptr);
char* newlineChar = strtok(myString, "\n");
while (newlineChar != NULL) {
char* equalSign = strstr(newlineChar, "=");
if (equalSign != NULL) {
*equalSign = '\0'; //write a null byte at "="
// printf("VAR: %s === %s\n", lineCopy, equalSign + 1);
int length = snprintf( NULL, 0, "%s:%s", newlineChar, equalSign + 1);
char* str = malloc( length + 1 );
snprintf( str, length + 1, "%s:%s", newlineChar, equalSign + 1);
*equalSign = '='; //write an equal sign reverting the null byte
finalArgv[finalOffset] = "--var";
finalArgv[finalOffset + 1] = str;
finalOffset += 2;
}
newlineChar = strtok(NULL, "\n");
}
// for(int loop = 0; loop < 128; loop++) {
// char* el = finalArgv[loop];
// if (el == NULL) {
// break;
// };
// printf("%s ", el);
// }
free(myString);
fclose(fptr);
execvp("bunx", finalArgv);
printf("Oh no, something went wrong with execvp()! %s\n", strerror(errno));
}
This issue should be re-opened and considered as a change for the next major (or minor 🙏) release of Wrangler. It is a common issue that countless numbers of people run into. It is mind-boggling that Cloudflare refuses to accept the industry standard approach to defining environment variables for local development in a .env file, and has long-been my biggest issue with Wrangler.
Today, I was reading a user's blog post about their experience with next-on-pages and this was specifically called out as one of the pain points for local development.
You want people to be able to have environment variables to change how wrangler works? Okay. If those env vars are properly prefixed to demonstrate that they are specifically for wrangler, then it'll be fine. That can be a major version change, or a backwards compatible minor version change.
So that begs the question of why isn't this done? Why does cloudflare refuse to follow industry standard approaches to env vars for dev servers?
Prioritise good developer experience, not inconvenience.
CCing some people who might be interested: @IgorMinar @WalshyDev @dario-piotrowicz @penalosa
This issue should be re-opened and considered as a change for the next major (or minor 🙏) release of Wrangler. It is a common issue that countless numbers of people run into. It is mind-boggling that Cloudflare refuses to accept the industry standard approach to defining environment variables for local development in a .env file, and has long-been my biggest issue with Wrangler.
Today, I was reading a user's blog post about their experience with next-on-pages and this was specifically called out as one of the pain points for local development.
You want people to be able to have environment variables to change how wrangler works? Okay. If those env vars are properly prefixed to demonstrate that they are specifically for wrangler, then it'll be fine. That can be a major version change, or a backwards compatible minor version change.
So that begs the question of why isn't this done? Why does cloudflare refuse to follow industry standard approaches to env vars for dev servers?
Prioritise good developer experience, not inconvenience.
CCing some people who might be interested: @IgorMinar @WalshyDev @dario-piotrowicz @penalosa
Agree that this should be re-opened for the above reasons
Adding to @andywwright 's comment about how you could make this change, but you won't, I don't see any reason why you couldn't just use .env for wrangler system settings AND developer settings. I'm sure there is almost zero chance of overlap with the 9 system variables you can currently use in .env and if by chance there were, they would probably be the same value. The only 4 settings that a developer might use are the CLOUDFLARE_ prefixed ones and I would suspect they would want the same values in wrangler and their code.
For the WRANGLER_ prefixed ones, what are the chances someone would be using WRANGLER_HYPERDRIVE_LOCAL_CONNECTION_STRING_<BINDING_NAME> in their code, 1, and 2, would want it to have a different value?
I agree with @treeder’s take—using .env for both Wrangler system settings and developer variables makes sense and would simplify things. I also prefer the prefix method; it’s cleaner and reduces the chance of conflicts while keeping everything consistent. Hopefully, they’ll reopen the issues and consider this approach!
What do we want? Reopen the issue When do we want it? Now How do we want it? .env
I'm going to re-open this as we consider whether we can safely use .env for both Wrangler configuration & runtime environment variables. cc @irvinebroque @petebacondarwin
as an update on this, please check out the proposal @korinne has put up here: https://github.com/cloudflare/workers-sdk/discussions/9394. thanks everyone for your feedback and patience ❤
I believe this is done now?
Indeed! Thanks.