$env/static/private module provides access to environment variables that are only available on the server and are replaced at build time. These variables are statically analyzed and replaced during the build process, making them faster to access but requiring a rebuild to change.
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).Static Replacement
Unlike dynamic environment variables, static variables are replaced at build time. For example:Example: Server Load Function
Example: API Configuration
Example: Server Hook
Example: JSON Environment Variable
Configuration
You can configure which environment variables are included using theenv.privatePrefix option:
PRIVATE_ will be available in $env/static/private. Variables without a matching prefix are discarded.
Setting Private Environment Variables
Create a.env file in your project root:
When to Use Static vs Dynamic
Use Static
- Build-time constants that don’t change
- Better performance (no runtime lookup)
- Type-safe access with TypeScript
- Dead code elimination possible
Use Dynamic
- Values that change between deployments
- Different staging/production values
- Secrets not in build output
- Runtime configuration
TypeScript Support
SvelteKit automatically generates types for your static environment variables. When you import from$env/static/private, TypeScript will provide autocomplete and type checking:
Performance Benefits
Static environment variables offer several performance advantages:- No runtime lookup - Values are inlined during build
- Dead code elimination - Unused imports can be tree-shaken
- Smaller bundle - No need to ship environment variable runtime
Security Considerations
See Also
- $env/dynamic/private - Dynamic server-only environment variables
- $env/static/public - Static public environment variables
- Configuration - Environment variable configuration options