Skip to content

Commit d0674fa

Browse files
committed
fix: process.env has a higher loading priority than .env files
1 parent aef8e51 commit d0674fa

File tree

4 files changed

+10
-3
lines changed

4 files changed

+10
-3
lines changed
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
TEST_ENV_VAR=TEST_VALUE
1+
TEST_ENV_VAR=TEST_VALUE
2+
PROCESS_ENV_VAR=.ENV_FILE

examples/playground14/e2e/cloudflare.spec.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,9 @@ test.describe("playground/cloudflare", () => {
1313
const { nextConfig } = await res.json();
1414
expect(nextConfig.output).toEqual("standalone");
1515
});
16+
17+
test("Environment variable defined on process.env are not overridden by .env files", async ({ page }) => {
18+
const res = await page.request.get("/api/env");
19+
await expect(res.json()).resolves.toEqual(expect.objectContaining({ PROCESS_ENV_VAR: "process.env" }));
20+
});
1621
});

examples/playground14/wrangler.jsonc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
}
1616
],
1717
"vars": {
18-
"hello": "Hello World from the cloudflare context!"
18+
"hello": "Hello World from the cloudflare context!",
19+
"PROCESS_ENV_VAR": "process.env"
1920
}
2021
}

packages/cloudflare/src/cli/templates/worker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ function populateProcessEnv(url: URL, env: CloudflareEnv) {
7878
const mode = env.NEXTJS_ENV ?? "production";
7979
if (nextEnvVars[mode]) {
8080
for (const key in nextEnvVars[mode]) {
81-
process.env[key] = nextEnvVars[mode][key];
81+
process.env[key] ??= nextEnvVars[mode][key];
8282
}
8383
}
8484

0 commit comments

Comments
 (0)