# Next.js environment variables in production

`.env` files are never uploaded to Reverhost. Here is how to set variables for Next.js, and when each one takes effect.

## How Next.js reads variables

Variables prefixed with `NEXT_PUBLIC_` are inlined into browser code during `next build`, so set them before you deploy. Server code reads the rest at runtime.

## Set your variables

```
npx ezhst env set NEXT_PUBLIC_SITE_URL=https://my-app.easyhost.fun
npx ezhst env import .env
npx ezhst env list
npx ezhst redeploy
```

Variables apply on the next `deploy` or `redeploy`.

## What Reverhost provides

| Variable | Value | Available |
| --- | --- | --- |
| `PORT` | `3000` | Build step and runtime |
| `HOSTNAME` | `0.0.0.0` | When the app runs |

`NODE_ENV=production` comes from the `ENV` line in the Node.js Dockerfile template, and `HOST` is set only if your Dockerfile sets it.

Your own variables are runtime env in the container. During the build they exist only in a `RUN` step that mounts the `appenv` secret, like the build step in the template:

```Dockerfile
RUN --mount=type=secret,id=appenv,target=/run/secrets/appenv sh -c 'set -a; . /run/secrets/appenv; set +a; npm run build'
```

Never pass secrets with `ARG` or `ENV`, because they end up in the image.

## Common problems

- [Variable is undefined](/errors/env-missing-at-runtime) because it was only in a `.env` file
- [A public variable is undefined in the browser](/errors/public-env-undefined) because it changed after the build

See the [Next.js hub](/frameworks/nextjs) for every Next.js guide, or [free Next.js hosting](/free-nextjs-hosting) for what is included.

## Frequently asked questions

### Are my .env files uploaded?

No. `ezhst deploy` skips every `.env*` file. Use `ezhst env set` or `ezhst env import`.
