Environment Variables


Introduction

Your project can consume variables declared in your environment as if they were declared locally in your JS files. By default you will have NODE_ENV defined for you, and any other environment variables starting with VITE_.

Adding environment variables

To define permanent environment variables, create a file called .env in the root of your project:
VITE_NOT_SECRET_CODE=abcdef
Note: You need to restart the development server after changing .env files.

Accessing environment variables

Environment variables will be defined for you on import.meta.env. For example, having an environment variable named VITE_NOT_SECRET_CODE will be exposed in your JS as import.meta.env.VITE_NOT_SECRET_CODE.
if (import.meta.env.DEV) {
  // do something in development mode only
}
if (import.meta.env.PROD) {
  // do something in production mode only
}
<title>{import.meta.env.VITE_WEBSITE_NAME}</title>

Learn more

To learn more about environment variables, click here.