Skip to content

Dotenv Config ​

Experimental

The dotenv config and the .env.schema support are experimental and may change. See Env Files.

vp dev, vp build, vp preview, and vp test load and validate .env files against .env.schema when the file exists. The dotenv block in vite.config.ts controls that loading. See Env Files.

The defaults suit a project with .env.schema at its root. Most projects do not need this block.

Example ​

vite.config.ts
ts
import { defineConfig } from 'vite-plus';

export default defineConfig({
  dotenv: {
    // Load and validate `.env.schema`. Defaults to `'auto'`: on when the
    // project root has `.env.schema`, off otherwise.
    schema: 'auto',
    // Fail `vp build` when a sensitive value appears in the client output.
    // Defaults to true.
    scanBuildOutput: true,
    // How built server code gets its env. Defaults to `'init-only'`.
    ssrInjectMode: 'init-only',
  },
});

schema ​

  • Type: 'auto' | boolean
  • Default: 'auto'

Controls whether the schema loads:

  • 'auto': load the schema when the project root has .env.schema, or when its package.json sets varlock.loadPath.
  • true: require a schema. The command fails when neither exists. Use this in a project that must never run without validation.
  • false: never load a schema, even when the file exists. Vite's own .env handling applies.

The project root is the root option of the config, or the working directory. To keep the schema files in another directory, set varlock.loadPath in package.json. vp dotenv and the build use the same setting. Vite's envDir option is not supported while a schema is active.

scanBuildOutput ​

  • Type: boolean
  • Default: true

After vp build writes the client output, scan every emitted file for a plaintext copy of a sensitive value. On a match, the build fails and the scan output lists each match. Server output is not scanned. Set the option to false when every value is allowed in the output, or run vp dotenv scan yourself at another point in the pipeline.

ssrInjectMode ​

  • Type: 'init-only' | 'auto-load' | 'resolved-env'
  • Default: 'init-only'

Controls how the server code that vp build writes gets the resolved env. Vite+ passes this option to varlock's Vite integration, as varlockVitePlugin({ ssrInjectMode }) would:

  • 'init-only': the server reads the env that varlock run resolves. Start it with varlock run -- node dist/server.js.
  • 'auto-load': the server loads .env.schema when it starts, so start it with node as usual. The schema and the env files, or the variables that they read, must be present where the server runs.
  • 'resolved-env': the build writes the resolved values into the server output. The server needs no env at run time, but the output then holds each value, secrets included. See encrypted deployments in the varlock documentation to encrypt them.

In each mode, the server code imports varlock at run time, so keep it in dependencies, where vp dotenv init adds it. Client output is not affected: it gets only public values in every mode. See Server-side rendering.

Redaction ​

Log redaction and the HTTP response guard are configured in the schema, not in vite.config.ts. Use the @redact root decorator, or @sensitive={redact=false} on one variable. See the varlock documentation.