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.SOME_ENV to access environment variables.
// /pages/movies/+data.js// Environment: serverconst 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.ts// Environment: serverimport 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}
// components/ContactUs.jsx// Environment: client// This environment variable is available on the client because it's prefixed with PUBLIC_ENV__const email = import.meta.env.PUBLIC_ENV__CONTACT_EMAILfunction ContactUs() { return <p>Contact us at {email}</p>}
// ✅ Works, eveywhere and statically replaced (minimizing bundle sizes)import.meta.env.SOME_ENV// ⚠️ Works, but only on server-side and no static replacementprocess.env.SOME_ENV// ⚠️ Works, but only on server-side and no static replacementconst { SOME_ENV } = process.env