$env/dynamic/private module provides access to environment variables that are only available on the server at runtime. Unlike static environment variables, these are read from the process environment and can change between deployments without requiring a rebuild.
Usage
This module can only be imported in server-side code (e.g.,
+page.server.js, +layout.server.js, +server.js, and hooks.server.js).Environment Variable
An object containing all environment variables that match the
privatePrefix configuration (default: all non-public variables).The env object is read from process.env at runtime, allowing you to access environment variables that are set after the application is built.Example: Server Load Function
Example: API Route
Example: Remote Function
Configuration
You can configure which environment variables are included using theenv.privatePrefix option in your svelte.config.js:
PRIVATE_ will be available in $env/dynamic/private. Variables without a matching prefix are discarded.
When to Use Dynamic vs Static
Use Dynamic
- Environment variables that change between deployments
- Different values for staging/production
- Secrets that shouldn’t be in the build output
- Runtime configuration
Use Static
- Build-time constants
- Values that never change
- Better performance (inlined at build time)
- Type-safe access
Security Considerations
See Also
- $env/static/private - Static server-only environment variables
- $env/dynamic/public - Dynamic client-accessible environment variables
- Configuration - Environment variable configuration options