Side by side
| Next.js | Vite | |
|---|---|---|
| Public prefix | NEXT_PUBLIC_ | VITE_ (change with envPrefix) |
| Read in client code | process.env.NEXT_PUBLIC_X | import.meta.env.VITE_X |
| Read on the server | process.env.X (all variables) | SSR: import.meta.env / process.env as set by your server; config file: loadEnv() |
| Files | .env, .env.local, .env.[mode], .env.[mode].local | same names |
| Mode | NODE_ENV (development, production, test) | --mode (default development / production; Vitest uses test) |
| Built-in values | process.env.NODE_ENV | MODE, DEV, PROD, SSR, BASE_URL |
| Types | strings | strings (type them via ImportMetaEnv) |
| Change a value | rebuild | rebuild |
Why both use a prefix
A bundler can't know which variables are safe for browsers. A naming rule makes the decision visible in code review: NEXT_PUBLIC_STRIPE_SECRET_KEY or VITE_DATABASE_URL look wrong at a glance. Run the ENV leak checker on your .env before deploying to catch real credentials with a public prefix.
Code differences when porting
// Next.js client component const api = process.env.NEXT_PUBLIC_API_URL; const isProd = process.env.NODE_ENV === "production"; // Vite / React const api = import.meta.env.VITE_API_URL; const isProd = import.meta.env.PROD;
import { defineConfig, loadEnv } from "vite";
export default defineConfig(({ mode }) => {
const env = loadEnv(mode, process.cwd(), ""); // "" = load all keys, not only VITE_*
return {
server: { port: Number(env.PORT ?? 5173) },
envPrefix: ["VITE_", "PUBLIC_"], // optional: extra public prefixes
};
});interface ImportMetaEnv {
readonly VITE_API_URL: string;
}Shared gotchas
- Build-time inlining: changing a public value in your host's dashboard does nothing until you rebuild.
- Literal access only:
process.env[name]/import.meta.env[name]with a variable key isn't replaced in client code. - Restarts: Vite reads env files on start, so restart
viteafter edits;next devreloads env files itself. - One image, many environments: don't use public variables for values that differ per environment; serve them from the server at runtime instead.
Next.js specifics, including Docker build args and a runtime-config pattern: NEXT_PUBLIC_ variables explained. Templates: Next.js .env, React (Vite) .env.
Other frameworks
| Framework | Public prefix | Inlined at build? |
|---|---|---|
| Next.js | NEXT_PUBLIC_ | yes |
| Vite (React, Vue, Svelte, Solid) | VITE_ | yes |
| SvelteKit | PUBLIC_ | $env/static/public yes, $env/dynamic/public no |
| Nuxt | NUXT_PUBLIC_ | no: overrides runtimeConfig.public at runtime |
| Astro | PUBLIC_ | yes |
| Create React App (deprecated) | REACT_APP_ | yes |
Frequently asked questions
Can server code read NEXT_PUBLIC_ variables?
Yes. The prefix only decides what is additionally copied into the browser bundle; server components, route handlers and server actions see every variable.
Why is my VITE_ variable undefined?
Restart the dev server after editing .env, check the prefix (or your envPrefix), and read it as import.meta.env.VITE_NAME, not process.env.
Can I use NEXT_PUBLIC_ variables in a Vite app?
Only if you add the prefix to envPrefix (e.g. envPrefix: ["VITE_", "NEXT_PUBLIC_"]) and read them via import.meta.env. Renaming them to VITE_ is cleaner.
How do I change a public value without rebuilding?
With Next.js or Vite, you can't via env vars. Fetch it at runtime from an API route or render it from the server. Nuxt's runtime config and SvelteKit's $env/dynamic/public support runtime values natively.
Which should I pick, Next.js or Vite?
Env handling is nearly identical, so decide on the framework: server rendering and routing built in (Next.js) or a lean SPA/build tool you combine yourself (Vite).