Skip to main content

@sveltejs/kit/env

import { defineEnvVars } from '@sveltejs/kit/env';

defineEnvVars

Utility for defining environment variables, which are made available via $app/env/public and $app/env/private.

import { defineEnvVars } from '@sveltejs/kit/env';
import * as v from 'valibot';

export const variables = defineEnvVars({
	API_URL: {
		schema: v.pipe(v.string(), v.url())
	},
	PORT: {
		schema: (value) => {
			if (value === undefined) return 3000;
			const port = Number(value);
			if (!Number.isInteger(port)) throw new Error('PORT must be an integer');
			return port;
		}
	}
});
function defineEnvVars<
	T extends Record<string, EnvVarConfig<any>>
>(variables: T): DefinedEnvVars<T>;

DefinedEnvVars

The return type of defineEnvVars.

type DefinedEnvVars<
	T extends Record<string, EnvVarConfig<any>>
> = {
	readonly [K in keyof T]: EnvVarEntry<T[K]>;
};

EnvVarConfig

Environment variables can be configured by exporting a variables object from src/env.ts, using defineEnvVars.

interface EnvVarConfig<T> {/*…*/}
public?: boolean;
  • default false

Whether the environment variable can be accessed by client-side code.

  • if true, it can be imported from $app/env/public
  • if false, it can be imported from $app/env/private, which is a server-only module
static?: boolean;
  • default false

Whether the value is determined at build time or when the app runs.

  • if true, the build time value is inlined into the bundle. This enables optimisations like dead-code elimination
  • if false, the value is read from the environment when the app starts
schema?: StandardSchemaV1<string | undefined, T> | ((value: string | undefined) => T | undefined);

A Standard Schema validator that is applied to the value when the app starts. Alternatively, a function that returns the (possibly transformed) value, or throws an error explaining the problem. Returning undefined is valid, so a function can describe an optional variable. The validator can output any value — not necessarily a string — but public, non-static values must be serializable by devalue so that they can be sent to the browser.

If omitted, the value must be set, but may be an empty string.

description?: string;

A description of the variable that will be used for inline documentation on hover.

Edit this page on GitHub llms.txt