> Markdown version of [Use Environment Variables](https://vaadin.com/docs/next/building-apps/react/environment-variables). Section index: [llms.txt](https://vaadin.com/docs/next/building-apps/llms.txt)

# Use Environment Variables in React Views

A React view can read configuration from environment variables, such as the address of an external service that differs between development and production. Vaadin builds the frontend with Vite, so React views use Vite’s support for environment variables and `.env` files. This guide shows how to set it up in a Vaadin project.

Vite replaces each variable with its value when it builds the frontend. The value then becomes part of the JavaScript code that the browser downloads. This has two consequences:

- Anyone who opens the application can read the values. Never put passwords, API keys, or other secrets in them.

- Vite fixes the values when it builds the frontend. A build has the same values on every server that you deploy it to.

For values that are secret, or that differ between servers, see [Values That Differ Between Deployments](#runtime).

## <a id="defining-variables"></a>Defining Variables

Put the variables in a file called `.env` in the frontend folder, `src/main/frontend`:

`src/main/frontend/.env`

```properties
VITE_MAP_TILES_URL=https://tiles.example.com
```

Vaadin makes the frontend folder the root folder of Vite, and Vite reads `.env` files from its root folder. A `.env` file next to `pom.xml` has no effect, unless you configure Vite as described in [Keeping the Files in the Project Root](#project-root).

Only the variables whose names start with `VITE_` are available to the views. Vite leaves out the other variables, so that they don’t end up in the browser by accident.

Variables set in the environment also work, and take precedence over the values in the files. Set them where Vite runs: in the production build, or in the application when it starts in development mode. For example, a build server can set a value for a production build:

```terminal
VITE_MAP_TILES_URL=https://tiles.example.com mvn clean package
```

## <a id="reading-variables-in-a-view"></a>Reading Variables in a View

Read the variables from `import.meta.env`:

```tsx
export default function MapView() {
  const tilesUrl = import.meta.env.VITE_MAP_TILES_URL;
  // ...
}
```

A variable that Vite didn’t find is `undefined` in the view. When that happens, check that the `.env` file is in the frontend folder, and that the name of the variable starts with `VITE_`.

TypeScript needs to know the type of `import.meta.env`. Without it, the production build fails with the error `Property 'env' does not exist on type 'ImportMeta'`. Add a file called `vite-env.d.ts` to the frontend folder:

`src/main/frontend/vite-env.d.ts`

```typescript
/// <reference types="vite/client" />

interface ImportMetaEnv {
  readonly VITE_MAP_TILES_URL: string;
}
```

The first line adds the types that Vite provides, including those of its built-in variables, such as `import.meta.env.MODE`. The interface gives your own variables the type `string`. Without it, they have the type `any`.

## <a id="different-values-for-development-and-production"></a>Different Values for Development and Production

Vite also reads `.env.development` in development mode, and `.env.production` in a production build. Their values take precedence over the values in `.env`:

`src/main/frontend/.env.development`

```properties
VITE_MAP_TILES_URL=http://localhost:9000
```

In development mode, Vite restarts when a `.env` file changes. Reload the browser to get the new values.

For the other files that Vite reads, and the order of precedence among them, see [Environment Variables and Modes](https://vite.dev/guide/env-and-mode) in the Vite documentation.

## <a id="project-root"></a>Keeping the Files in the Project Root

To keep the `.env` files next to `pom.xml`, set the Vite `envDir` option in the `vite.config.ts` file in the project root:

`vite.config.ts`

```typescript
import type { UserConfigFn } from 'vite';
import { overrideVaadinConfig } from './vite.generated.ts';

const customConfig: UserConfigFn = (env) => ({
  envDir: import.meta.dirname
});

export default overrideVaadinConfig(customConfig);
```

`import.meta.dirname` is the folder of `vite.config.ts`. Vite then reads the `.env` files from the project root, and no longer from the frontend folder.

Vaadin generates `vite.config.ts` if the file doesn’t exist yet, and doesn’t overwrite your changes. Commit the file to source control.

## <a id="runtime"></a>Values That Differ Between Deployments

Keep secrets, and values that differ between servers that run the same build, on the server. Pass them to the application from outside the build, for example as environment variables or mounted files. See [Configure the Application](https://vaadin.com/docs/next/building-apps/production/configure.md).

A React view gets these values by calling a browser-callable service, which returns only what the view needs. A secret, such as the API key of an external service, stays on the server: the service uses the key to call the external service on behalf of the view. See [Call Java Services](https://vaadin.com/docs/next/building-apps/react/call-services.md).
