[PR #234] [MERGED] fix: load .env files into process.env (closes #228) #407

Closed
opened 2026-05-06 12:39:39 +02:00 by BreizhHardware · 0 comments

📋 Pull Request Information

Original PR: https://github.com/cloudflare/vinext/pull/234
Author: @southpolesteve
Created: 3/3/2026
Status: Merged
Merged: 3/3/2026
Merged by: @southpolesteve

Base: mainHead: fix/dotenv-loading


📝 Commits (1)

  • 2e32dc9 fix: load .env files into process.env before config evaluation

📊 Changes

2 files changed (+143 additions, -2 deletions)

View changed files

📝 packages/vinext/src/index.ts (+16 -2)
📝 tests/features.test.ts (+127 -0)

📄 Description

Summary

Fixes #228. vinext was not loading .env files, so environment variables defined in .env were unavailable everywhere: server-side code (getServerSideProps, API routes, server components), NEXT_PUBLIC_* client bundle defines, and next.config.js evaluation.

  • Calls Vite's loadEnv() early in the config hook (before loadNextConfig()) with an empty prefix to load all .env vars into process.env, matching Next.js behavior
  • Respects existing process.env values (system env takes precedence over .env files)
  • Respects Vite's envDir config option
  • Adds two integration tests covering both NEXT_PUBLIC_* define injection and server-side process.env access

Changes

packages/vinext/src/index.ts (the fix, 12 lines):

  • Import loadEnv from Vite
  • In the config hook, call loadEnv(mode, envDir, "") and merge results into process.env before config loading

tests/features.test.ts (reproduction tests):

  • Creates a temp project with a .env file containing NEXT_PUBLIC_APP_NAME, SERVER_ONLY_SECRET, and BETTER_AUTH_URL
  • Verifies NEXT_PUBLIC_* vars appear in Vite's config.define map
  • Verifies getServerSideProps can read all .env vars via process.env

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/cloudflare/vinext/pull/234 **Author:** [@southpolesteve](https://github.com/southpolesteve) **Created:** 3/3/2026 **Status:** ✅ Merged **Merged:** 3/3/2026 **Merged by:** [@southpolesteve](https://github.com/southpolesteve) **Base:** `main` ← **Head:** `fix/dotenv-loading` --- ### 📝 Commits (1) - [`2e32dc9`](https://github.com/cloudflare/vinext/commit/2e32dc9cfe715f1a197c492a4c4a7db0c6b1747d) fix: load .env files into process.env before config evaluation ### 📊 Changes **2 files changed** (+143 additions, -2 deletions) <details> <summary>View changed files</summary> 📝 `packages/vinext/src/index.ts` (+16 -2) 📝 `tests/features.test.ts` (+127 -0) </details> ### 📄 Description ## Summary Fixes #228. vinext was not loading `.env` files, so environment variables defined in `.env` were unavailable everywhere: server-side code (`getServerSideProps`, API routes, server components), `NEXT_PUBLIC_*` client bundle defines, and `next.config.js` evaluation. - Calls Vite's `loadEnv()` early in the `config` hook (before `loadNextConfig()`) with an empty prefix to load all `.env` vars into `process.env`, matching Next.js behavior - Respects existing `process.env` values (system env takes precedence over `.env` files) - Respects Vite's `envDir` config option - Adds two integration tests covering both `NEXT_PUBLIC_*` define injection and server-side `process.env` access ## Changes **`packages/vinext/src/index.ts`** (the fix, 12 lines): - Import `loadEnv` from Vite - In the `config` hook, call `loadEnv(mode, envDir, "")` and merge results into `process.env` before config loading **`tests/features.test.ts`** (reproduction tests): - Creates a temp project with a `.env` file containing `NEXT_PUBLIC_APP_NAME`, `SERVER_ONLY_SECRET`, and `BETTER_AUTH_URL` - Verifies `NEXT_PUBLIC_*` vars appear in Vite's `config.define` map - Verifies `getServerSideProps` can read all `.env` vars via `process.env` --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
BreizhHardware 2026-05-06 12:39:39 +02:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/vinext#407
No description provided.