Environment Rules
Rule
Scope: All apps and packages.
Goals
- Single source of truth for environment schemas.
- No ad-hoc
process.envreads in app/package code. - Clear separation between server-only vars and client-safe vars.
Packages vs Apps
- MUSTIn packages and server libraries, import from a shared env package (e.g.,
@project/env/server). - SHOULDIn Next.js apps, import from the env package's next export (e.g.,
@project/env/next). - SHOULDApps create their own
env.tsfiles for app-specific variables. - NEVERRead
process.env.Xdirectly in feature code. Only the env schema files should touchprocess.env.
Schemas and Sources
- Shared package:
packages/env/server— server-only variables (Node/server contexts)/next— Next.js app variables (server + client)
- App-local schema:
apps/<app>/env.tsfor app-specific variables (optional).
Client vs Server
- MUSTClient-available vars MUST be prefixed
NEXT_PUBLIC_. - SHOULDNarrow exposure: only export what the client needs.
Adding Variables
- Decide the scope and add the key to the correct schema.
- Update
runtimeEnvmapping for every added key. - Use the typed import in code (no direct
process.env).
Safety
- NEVERLog secrets. See security.md.
- NEVERCommit
.envfiles. - SHOULDFail fast on validation errors (default behavior).