Skip to content

Adding environment variables in a Vite React app

Vite is designed for front-end React development, so the notion of Node.js' process module doesn't really apply. How then does one create and access process.env environment variables? Thankfully it's surprisingly straightforward. Firstly create a .env file in your project root and put, say, the following into it:

BASE_URL = https://some.url.here

Then you can access that via Vite's own env variables object import.meta.env:

...
const url = import.meta.env.BASE_URL
...

Except that will not work. Here url will be undefined. To ensure that Vite collects and env variables meant for it, prefix your var names with VITE_:

VITE_BASE_URL = https://some.url.here
...
const url = import.meta.env.VITE_BASE_URL
...