Environment (.env) files and .gitignore #22287
Replies: 8 comments 1 reply
|
#19471 is related but does not provide a clear answer. |
|
Another example from the twelve factor app:
|
|
Your instinct is right and the Vite docs aren't really contradicting the "don't commit Vite treats To make this concrete: Concretely: Two rules that keep this safe:
How this compares to the dotenv FAQThe generic dotenv advice ("don't commit
If your team comes from a plain-dotenv background, a one-line |
|
Thanksi @MukundaKatta, I appreciate the quick response and detailed answer. 🙂 Just out of curiosity: Why not use a dedicated file type (without |
|
To safely handle your environment variables in a Vite project without leaking secrets to , follow this standard setup: 1. Update your
|
|
You're reading the Vite docs correctly — and the apparent contradiction with general dotenv advice is intentional. Vite makes a deliberate design choice here that's worth understanding. The Vite Model: Split By Purpose, Not By NameVite distinguishes between shared defaults (committed) and local overrides/secrets (ignored):
Vite's Why This Is Different From dotenv's "Never Commit"The dotenv FAQ is really saying: don't commit secrets. The method it suggests (ignore everything) is conservative. Vite's approach is more precise: the The key distinction: # .env — SAFE TO COMMIT (no secrets)
VITE_API_URL=https://api.example.com
VITE_APP_NAME=My App
VITE_FEATURE_FLAGS_ENABLED=true
# .env.local — NOT committed (personal/secret values)
VITE_API_KEY=sk-my-real-key # ❌ don't put this in .env!
DATABASE_URL=postgres://...The
|
|
You are not wrong to feel confused — this is a genuine distinction that Vite makes intentionally, and it differs from the pure dotenv convention. Vite's mental model:
The key insight is: only So the convention is:
Best practice for teams:
This is different from the dotenv-for-Node.js convention because in a Node app, |
|
The confusion here is real — I hit the same wall when I first started with Vite. The short version: Vite's Why Vite commits Generic dotenv advice ("never commit Vite's situation is fundamentally different: it's a frontend build tool. Anything that ends up in Given that, Vite splits env files by audience, not by sensitivity:
The Your practical setup For actual secrets that your backend needs? Don't put them in Vite env files at all — they belong in server environment variables (CI/CD secrets, platform config), not in anything with a The Commit an Add one line to your README: "copy To answer your Spring comparison — the reason Vite doesn't use a separate file type is it leverages the existing OS/shell Both docs are correct simultaneously:
If your team comes from a plain-dotenv background, a one-liner in the README ("Vite-style: commit |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Nearly every source on the web advises against committing any
.envfiles into git (or other version control system).For example, here's from the dotenv FAQ:
This makes perfect sense to me.
I assumed that ignoring
.envfiles was a general "best practice," and, knowing that vite uses dotenv, I expected this to apply to vite as well.However, -- please correct me if I'm wrong -- contrary to the above, the vite docs appear to suggest that
.envfiles should be committed, except for.env*.localfiles:and
This comes as a surprise, and it sounds rather risky to me.
For example, what happens if a new dev, like me, assumes all
.envfiles are ignored, and is unaware of the distinction between.envand.env.local?Could someone shed some light on this?
All reactions