Feature Request: Type-Safe Environment Variables with Built-in Validation and Parsing #79662
Replies: 2 comments
-
You might be interested in https://varlock.dev - and its Next.js integration It's not exactly the same as what you are proposing but it does provide
It also removes the need for a prefix, or thinking about client/server, and just asks the user to specify whether items are sensitive or not. While it is an additional tool, the goal of the project is to provide a universal toolkit, so you dont need a different config system on every project. Additionally, the next integration is implemented as a drop-in replacement for Would love to hear what you think, and very open to collaboration :) |
Beta Was this translation helpful? Give feedback.
-
could I add, optional dynamic client variables that copy current value from server, rather than hardcoding them on build time? |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Goals
env.ts
(orsrc/env.ts
) file to centralize environment variable definition, parsing, and validation.process.env
access or incorrect assumptions about variable types or presence.Non-Goals
.env
files: This feature would work alongside existing.env
file loading mechanisms, providing a typed layer on top.env.ts
file itself, processed by SWC.NEXT_PUBLIC_
prefix requirement for client-side variables: This convention will still be respected and enforced.Background
Currently, Next.js loads environment variables from
.env
files and differentiates client-side variables using theNEXT_PUBLIC_
prefix. While functional, this approach has a few pain points for developers:process.env.MY_VAR
offers no type information, leading to potential type errors at runtime (e.g., expecting a number but getting a string or undefined). Autocompletion is also unavailable.NEXT_PUBLIC_
variable is missing, the application might start but then fail unexpectedly when the variable is accessed.parseInt(process.env.PORT)
,process.env.FEATURE_FLAG === 'true'
) and handle potential errors.Many developers address these issues by integrating third-party libraries like Zod, T3 Env, or by writing custom scripts. However, having a built-in, optional mechanism within Next.js would streamline this process, improve the out-of-the-box developer experience, and encourage best practices for environment variable management.
This proposal aims to provide a first-class, albeit experimental, solution within Next.js itself, leveraging its existing SWC infrastructure for transpilation.
Proposal
I propose introducing an experimental flag in
next.config.js
, for example,experimental: { typedEnvs: true }
. When enabled, Next.js would look for anenv.ts
(orsrc/env.ts
) file.1.
env.ts
File Structure:This file would export a typed object, potentially after performing parsing and validation:
2. Next.js Integration:
NextEnv
type:typedEnvs
is true), Next.js would:env.ts
and parse it.env.client
object (and only those prefixed withNEXT_PUBLIC_
) would be inlined into the client bundle, accessible viaprocess.env
or the importedenv.client
.env.server
andenv.edge
objects would be available for import in their respective runtimes.env
object:3. Error Handling:
env.ts
validation fails.env.server.SOME_KEY
on the client would result inenv.server
being undefined (or the access proxied to throw an error).Benefits of this approach:
Yes, I am interested in contributing to the implementation of this feature if it's well-received by the community and maintainers. I'm happy to discuss the technical details further and refine the proposal based on feedback.
Beta Was this translation helpful? Give feedback.
All reactions