$env/dynamic/public
This module provides access to environment variables set dynamically at runtime and that are publicly accessible.
| Runtime | Build time | |
|---|---|---|
| Private | $env/dynamic/private |
$env/static/private |
| Public | $env/dynamic/public |
$env/static/public |
Dynamic environment variables are defined by the platform you're running on. For example if you're using adapter-node (or running vite preview), this is equivalent to process.env.
Public access:
- This module can be imported into client-side code
- Only variables that begin with
config.kit.env.publicPrefix(which defaults toPUBLIC_) are included
In
dev,$env/dynamicincludes environment variables from.env. Inprod, this behavior will depend on your adapter.
To get correct types, environment variables referenced in your code should be declared (for example in an
.envfile), even if they don't have a value until the app is deployed:MY_FEATURE_FLAG=You can override
.envvalues from the command line like so:MY_FEATURE_FLAG="enabled" npm run dev
For example, given the following runtime environment:
ENVIRONMENT=production
PUBLIC_BASE_URL=http://example.comWith the default publicPrefix and privatePrefix:
import { import envenv } from '$env/dynamic/public';
var console: Consoleconsole.Console.log(...data: any[]): voidlog(import envenv.ENVIRONMENT); // => undefined, not public
var console: Consoleconsole.Console.log(...data: any[]): voidlog(import envenv.PUBLIC_BASE_URL); // => "http://example.com"Edit this page on GitHub llms.txt