{"id":34,"date":"2025-08-08T01:00:00","date_gmt":"2025-08-08T01:00:00","guid":{"rendered":"https:\/\/wordpress.securinsight.ca\/index.php\/2025\/08\/08\/workers-wrangler-and-the-cloudflare-vite-plugin-support-env-files-in-local-development-2\/"},"modified":"2025-08-08T01:00:00","modified_gmt":"2025-08-08T01:00:00","slug":"workers-wrangler-and-the-cloudflare-vite-plugin-support-env-files-in-local-development-2","status":"publish","type":"post","link":"https:\/\/wordpress.securinsight.ca\/index.php\/2025\/08\/08\/workers-wrangler-and-the-cloudflare-vite-plugin-support-env-files-in-local-development-2\/","title":{"rendered":"Workers &#8211; Wrangler and the Cloudflare Vite plugin support `.env` files in local development"},"content":{"rendered":"<p>Now, you can use <code>.env<\/code> files to provide secrets and override environment variables on the <code>env<\/code> object during local development with Wrangler and the Cloudflare Vite plugin.<\/p>\n<p>Previously in local development, if you wanted to provide secrets or environment variables during local development, you had to use <code>.dev.vars<\/code> files.<br \/>\nThis is still supported, but you can now also use <code>.env<\/code> files, which are more familiar to many developers.<\/p>\n<h4>Using <code>.env<\/code> files in local development<\/h4>\n<p>You can create a <code>.env<\/code> file in your project root to define environment variables that will be used when running <code>wrangler dev<\/code> or <code>vite dev<\/code>. The <code>.env<\/code> file should be formatted like a <code>dotenv<\/code> file, such as <code>KEY=\"VALUE\"<\/code>:<\/p>\n<div>\n<figure>\n<pre data-language=\"bash\"><code class=\"language-bash\"><div><div><span>TITLE<\/span><span>=<\/span><span>\"My Worker\"<\/span><\/div><\/div><div><div><span>API_TOKEN<\/span><span>=<\/span><span>\"dev-token\"<\/span><\/div><\/div><\/code><\/pre>\n<div><\/div>\n<\/figure>\n<\/div>\n<p>When you run <code>wrangler dev<\/code> or <code>vite dev<\/code>, the environment variables defined in the <code>.env<\/code> file will be available in your Worker code via the <code>env<\/code> object:<\/p>\n<div>\n<figure>\n<pre data-language=\"javascript\"><code class=\"language-javascript\"><div><div><span>export<\/span><span> <\/span><span>default<\/span><span> <\/span><span>{<\/span><\/div><\/div><div><div><span>  <\/span><span>async<\/span><span> <\/span><span>fetch<\/span><span>(<\/span><span>request<\/span><span>,<\/span><span> <\/span><span>env<\/span><span>)<\/span><span> <\/span><span>{<\/span><\/div><\/div><div><div><span>    <\/span><span>const<\/span><span> <\/span><span>title<\/span><span> <\/span><span>=<\/span><span> <\/span><span>env<\/span><span>.<\/span><span>TITLE<\/span><span>;<\/span><span> <\/span><span>\/\/ \"My Worker\"<\/span><\/div><\/div><div><div><span>    <\/span><span>const<\/span><span> <\/span><span>apiToken<\/span><span> <\/span><span>=<\/span><span> <\/span><span>env<\/span><span>.<\/span><span>API_TOKEN<\/span><span>;<\/span><span> <\/span><span>\/\/ \"dev-token\"<\/span><\/div><\/div><div><div><span>    <\/span><span>const<\/span><span> <\/span><span>response<\/span><span> <\/span><span>=<\/span><span> <\/span><span>await<\/span><span> <\/span><span>fetch<\/span><span>(<\/span><\/div><\/div><div><div><span>      <\/span><span>`https:\/\/api.example.com\/data?token=<\/span><span>${<\/span><span>apiToken<\/span><span>}<\/span><span>`<\/span><span>,<\/span><\/div><\/div><div><div><span><span>    <\/span><\/span><span>)<\/span><span>;<\/span><\/div><\/div><div><div><span>    <\/span><span>return<\/span><span> <\/span><span>new<\/span><span> <\/span><span>Response<\/span><span>(<\/span><span>`Title: <\/span><span>${<\/span><span>title<\/span><span>}<\/span><span> - `<\/span><span> <\/span><span>+<\/span><span> (<\/span><span>await<\/span><span> <\/span><span>response<\/span><span>.<\/span><span>text<\/span><span>()))<\/span><span>;<\/span><\/div><\/div><div><div><span>  <\/span><span>},<\/span><\/div><\/div><div><div><span>};<\/span><\/div><\/div><\/code><\/pre>\n<div><\/div>\n<\/figure>\n<\/div>\n<h4>Multiple environments with <code>.env<\/code> files<\/h4>\n<p>You may be using <a href=\"https:\/\/developers.cloudflare.com\/workers\/wrangler\/environments\/\">Cloudflare Environments<\/a> to deploy different versions of a Worker with distinct environment variables. For instance, you may have a production and staging environment.<\/p>\n<p>To set different environment variables for each Cloudflare Environment, create files named <code>.env.&lt;environment-name&gt;<\/code>.<\/p>\n<p>When you use <code>wrangler &lt;command&gt; --env &lt;environment-name&gt;<\/code> or <code>CLOUDFLARE_ENV=&lt;environment-name&gt; vite dev<\/code>, the corresponding environment-specific file will also be loaded and merged with the <code>.env<\/code> file.<\/p>\n<p>For example, if you want to set different environment variables for the <code>staging<\/code> environment, you can create a file named <code>.env.staging<\/code>:<\/p>\n<div>\n<figure>\n<pre data-language=\"bash\"><code class=\"language-bash\"><div><div><span>API_TOKEN<\/span><span>=<\/span><span>\"staging-token\"<\/span><\/div><\/div><\/code><\/pre>\n<div><\/div>\n<\/figure>\n<\/div>\n<p>When you run <code>wrangler dev --env staging<\/code> or <code>CLOUDFLARE_ENV=staging vite dev<\/code>, the environment variables from <code>.env.staging<\/code> will be merged onto those from <code>.env<\/code>.<\/p>\n<div>\n<figure>\n<pre data-language=\"javascript\"><code class=\"language-javascript\"><div><div><span>export<\/span><span> <\/span><span>default<\/span><span> <\/span><span>{<\/span><\/div><\/div><div><div><span>  <\/span><span>async<\/span><span> <\/span><span>fetch<\/span><span>(<\/span><span>request<\/span><span>,<\/span><span> <\/span><span>env<\/span><span>)<\/span><span> <\/span><span>{<\/span><\/div><\/div><div><div><span>    <\/span><span>const<\/span><span> <\/span><span>title<\/span><span> <\/span><span>=<\/span><span> <\/span><span>env<\/span><span>.<\/span><span>TITLE<\/span><span>;<\/span><span> <\/span><span>\/\/ \"My Worker\" (from `.env`)<\/span><\/div><\/div><div><div><span>    <\/span><span>const<\/span><span> <\/span><span>apiToken<\/span><span> <\/span><span>=<\/span><span> <\/span><span>env<\/span><span>.<\/span><span>API_TOKEN<\/span><span>;<\/span><span> <\/span><span>\/\/ \"staging-token\" (from `.env.staging`, overriding the value from `.env`)<\/span><\/div><\/div><div><div><span>    <\/span><span>const<\/span><span> <\/span><span>response<\/span><span> <\/span><span>=<\/span><span> <\/span><span>await<\/span><span> <\/span><span>fetch<\/span><span>(<\/span><\/div><\/div><div><div><span>      <\/span><span>`https:\/\/api.example.com\/data?token=<\/span><span>${<\/span><span>apiToken<\/span><span>}<\/span><span>`<\/span><span>,<\/span><\/div><\/div><div><div><span><span>    <\/span><\/span><span>)<\/span><span>;<\/span><\/div><\/div><div><div><span>    <\/span><span>return<\/span><span> <\/span><span>new<\/span><span> <\/span><span>Response<\/span><span>(<\/span><span>`Title: <\/span><span>${<\/span><span>title<\/span><span>}<\/span><span> - `<\/span><span> <\/span><span>+<\/span><span> (<\/span><span>await<\/span><span> <\/span><span>response<\/span><span>.<\/span><span>text<\/span><span>()))<\/span><span>;<\/span><\/div><\/div><div><div><span>  <\/span><span>},<\/span><\/div><\/div><div><div><span>};<\/span><\/div><\/div><\/code><\/pre>\n<div><\/div>\n<\/figure>\n<\/div>\n<h4>Find out more<\/h4>\n<p>For more information on how to use <code>.env<\/code> files with Wrangler and the Cloudflare Vite plugin, see the following documentation:<\/p>\n<ul>\n<li><a href=\"https:\/\/developers.cloudflare.com\/workers\/development-testing\/environment-variables\">Environment variables and secrets<\/a><\/li>\n<li><a href=\"https:\/\/developers.cloudflare.com\/workers\/wrangler\" target=\"_blank\">Wrangler Documentation<\/a><\/li>\n<li><a href=\"https:\/\/developers.cloudflare.com\/workers\/wrangler\/vite\" target=\"_blank\">Cloudflare Vite Plugin Documentation<\/a><\/li>\n<\/ul>","protected":false},"excerpt":{"rendered":"<p>Now, you can use .env files to provide secrets and override environment variables on the env object during local development with Wrangler and the Cloudflare Vite plugin. Previously in local development, if you wanted to provide secrets or environment variables during local development, you had to use .dev.vars files. This is still supported, but you [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-34","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/wordpress.securinsight.ca\/index.php\/wp-json\/wp\/v2\/posts\/34","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wordpress.securinsight.ca\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wordpress.securinsight.ca\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wordpress.securinsight.ca\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/wordpress.securinsight.ca\/index.php\/wp-json\/wp\/v2\/comments?post=34"}],"version-history":[{"count":0,"href":"https:\/\/wordpress.securinsight.ca\/index.php\/wp-json\/wp\/v2\/posts\/34\/revisions"}],"wp:attachment":[{"href":"https:\/\/wordpress.securinsight.ca\/index.php\/wp-json\/wp\/v2\/media?parent=34"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wordpress.securinsight.ca\/index.php\/wp-json\/wp\/v2\/categories?post=34"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wordpress.securinsight.ca\/index.php\/wp-json\/wp\/v2\/tags?post=34"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}