Environment variables
Edit this pageSolid is built on top of Vite, which offers a convenient way to handle environment variables.
Public Environment Variables
Public variables are considered safe to expose to the client-side code. These variables are prefixed with VITE_
and are injected into the client-side code during compilation time.
In the root directory of the project, create a file called .env
.
This file will store environment variables in the key = value
format.
If working with TypeScript it is possible to make such variables type-safe and enable your TypeScript Language Service Provider (LSP) to autocomplete them by creating a file called .env.d.ts
in the root directory of the project.
To prevent accidental exposure of environment variables to the client, only variables prefixed with VITE_
will be exposed.
For example:
Only the VITE_SECRET_KEY
will be exposed to client source code, while DB_PASSWORD
will not, as shown below.
Private Environment Variables
These variables should only be accessed in your backend code, and so it's best to not use VITE_
prefix for them. Instead, use process.env
to access them. Depending on the Nitro preset chosen, they'll be made available automatically or they will require an external dependency such as dotenv.
To access them, within your backend code, use process.env
.
For an example, check the pseudo-code below.
It is also possible to make process.env
type-safe via the same .env.d.ts
file.