fix: let Next.js auto-import .env instead of manual dotenv setup#27939
fix: let Next.js auto-import .env instead of manual dotenv setup#27939hariombalhara wants to merge 3 commits intomainfrom
Conversation
|
Hey there and thank you for opening this pull request! 👋🏼 We require pull request titles to follow the Conventional Commits specification and it looks like your proposed title needs to be adjusted. Details: |
| @@ -1,5 +1,5 @@ | |||
| import process from "node:process"; | |||
| import { withBotId } from "botid/next/config"; | |||
There was a problem hiding this comment.
🔴 Symlink breaks environment variable loading on Windows (default Git settings)
On Windows, Git's core.symlinks defaults to false. When cloning this repo, Git will check out apps/web/.env as a plain text file containing the literal string ../../.env, rather than creating a real filesystem symlink. Next.js will then attempt to parse this plain text file as a .env file, find no valid KEY=VALUE pairs (dotenv skips lines without =), and load zero environment variables.
Root Cause and Impact
The previous code at apps/web/next.config.ts explicitly called dotenvConfig({ path: "../../.env" }), which resolved the file path correctly on all platforms. The new approach relies on a symlink (apps/web/.env -> ../../.env) that Next.js picks up automatically, but this symlink only works as intended on systems where Git creates real symlinks (macOS, Linux, or Windows with core.symlinks=true).
On a standard Windows setup:
apps/web/.envis a regular file containing the text../../.env- Next.js reads this file via its built-in
@next/envloader before evaluatingnext.config.ts - The dotenv parser sees
../../.env— a line with no=— and skips it - No env vars are populated into
process.env - The app immediately crashes at
apps/web/next.config.ts:48:if (!process.env.NEXTAUTH_SECRET) throw new Error("Please set NEXTAUTH_SECRET");
Impact: Any developer on Windows with default Git settings cannot run yarn dev or yarn build for apps/web. The app crashes on startup with "Please set NEXTAUTH_SECRET" even though the root .env file is correctly configured.
Prompt for agents
The symlink approach (apps/web/.env -> ../../.env) does not work on Windows where Git defaults to core.symlinks=false. Consider one of these alternatives:
1. Restore the explicit dotenv loading in apps/web/next.config.ts (revert the change):
- Re-add `import { config as dotenvConfig } from "dotenv";`
- Re-add `dotenvConfig({ path: "../../.env" });` before any process.env access
- The `import process from "node:process"` can stay or be removed (process is a global in Node.js)
2. Use a cross-platform file copy/generation script instead of a symlink:
- Add a pre-dev/pre-build script that copies ../../.env to apps/web/.env
- Keep apps/web/.env in .gitignore (as it already is)
3. If keeping the symlink approach, document that Windows users must either:
- Enable Developer Mode and set `git config --global core.symlinks true` before cloning
- Or manually copy the root .env file to apps/web/.env
Was this helpful? React with 👍 or 👎 to provide feedback.
What does this PR do?
Replaces the manual
dotenvconfiguration inapps/web/next.config.tswith Next.js's built-in.envauto-loading by adding a symlink atapps/web/.env → ../../.env.Changes:
dotenvimport anddotenvConfig({ path: "../../.env" })call fromnext.config.tsapps/web/.envpointing to the root.envso Next.js picks it up automaticallyimport process from "node:process"sinceprocessis no longer a side effect of the dotenv importMandatory Tasks (DO NOT REMOVE)
How should this be tested?
.envhas environment variables set (e.g.NEXT_PUBLIC_WEBAPP_URL)yarn devfromapps/web— verify env vars are loaded correctly and the app starts without errorsyarn buildforapps/web— verify the build succeeds and env vars are resolved at build timeHuman Review Checklist
.envsymlink is not excluded by.gitignore(it's tracked in this diff, but worth confirming)next.config.tsis evaluated (Next.js loads.envbefore config)dotenvdependency can now be removed fromapps/web/package.jsonChecklist