Environment variables can be defined in .env, .env.development, and .env.production files:
# .env# This environment variable is available only on the server-sideDATABASE_PASSWORD=e9wd!1hw1@#231# This environment variable is available on both the server- and client-sidePUBLIC_ENV__CONTACT_EMAIL=hello@example.org
Only environment variables prefixed with PUBLIC_ENV__ are available on the client-side.
⚠️
PUBLIC_ENV__* environment variables must never contain secret information — all PUBLIC_ENV__* values are visible to everyone because they are included in your client bundles which are public.
# .env.development# This environment variable is available only in developmentDATABASE_URL=postgresql://localhost:5432
# .env.production# This environment variable is available only in productionDATABASE_URL=postgresql://database.example.com:5432
If your repository is public (e.g. on GitHub), then make sure your .env files never contain secret information and, instead, consider defining secrets using:
Use import.meta.env to access environment variables.
// /pages/movies/+data.jsconst sql = new SQL({ // These environment variable are available here because +data runs on the server-side username: import.meta.env.DATABASE_URL, password: import.meta.env.DATABASE_PASSWORD})export async function data(pageContext) { const { movies } = await sql.run('SELECT * FROM movies;') return movies}
// /pages/movies/+data.tsimport type { PageContextServer } from 'vike/types'const sql = new SQL({ // These environment variable are available here because +data runs on the server-side username: import.meta.env.DATABASE_URL, password: import.meta.env.DATABASE_PASSWORD})export async function data(pageContext: PageContextServer) { const { movies } = await sql.run('SELECT * FROM movies;') return movies}
For server only code, you can access environment variables using process.env, but we generally recommend against this because it won't be statically replaced.
// components/ContactUs.jsx// The UI component <ContactUs> is loaded on the client-side// This environment variable is available on the client-side: it's prefixed with PUBLIC_ENV__const email = import.meta.env.PUBLIC_ENV__CONTACT_EMAILfunction ContactUs() { return <p>Contact us at {email}</p>}