Installation
Install the core package and get started with envin.
The core package can be used in any framework of your choice. To use it, figure out what prefix your framework uses for exposing environment variables to the client. For example, Astro uses PUBLIC_*
, while Vite uses VITE_*
. You should be able to find this in the framework's documentation.
Install dependencies
First, install the core package:
Then install a schema validator. envin
supports any validator that implements the Standard Schema specification:
Although we'll use Zod as examples throughout these docs, you can use any validator that supports Standard Schema, including Valibot, ArkType, and others.
envin
requires a minimum of typescript@5.0.0
.envin
is an ESM only package. Make sure that your tsconfig uses a module
resolution that can read package.json#exports
(Bundler
is recommended).
Create your schema
Create an environment configuration file (we recommend env.config.ts
, but you can name it whatever you want):
Some frameworks generate an env.d.ts
file that will collide with env.ts
,
which means you may need to name it something else like env.config.ts
.
If you only need server-side variables, you can omit the client
and clientPrefix
properties:
While defining both client and server schemas in a single file provides the best developer experience, it also means that your validation schemas for the server variables will be shipped to the client. If you consider the names of your variables sensitive, you should split your schemas into separate files.
For all available options, see Customization.
You'll notice that if your clientPrefix is PUBLIC_
, you won't be allowed to enter any other keys in the client object without getting type-errors. Below you can see we get a descriptive error when we set VITE_PUBLIC_API_URL
instead of PUBLIC_API_URL
:
Validate on build (recommended)
The steps required to validate your schema on build will vary from framework to framework, but you'll usually be able to import the env file in your configuration file, or in any file that's pulled in at the beginning of the build process.
For Next.js, you can import your env config in next.config.ts
:
Note that some frameworks don't import their environment variables in their configuration file, so you may need to import your env config in a different location.
Use your environment variables
Import the env object in your application and use it with full type-safety and auto-completion:
Strict runtime - envStrict
Exactly one of envStrict
or env
must be provided.
If your framework doesn't bundle all environment variables by default, but instead only bundles the ones you use, you can use the envStrict
option to ensure you don't forget to add any variables to your runtime:
When using the strict option, missing any of the variables in envStrict
will result in a type error.