Consider this Svelte component that imports a private environment variable and displays it:
<script>
import { PRIVATE_API_KEY } from '$env/static/private';
</script>
<p>API Key: {PRIVATE_API_KEY}</p>If the environment variable PRIVATE_API_KEY is set to abc123 at build time, what will the component render in the browser?
<script> import { PRIVATE_API_KEY } from '$env/static/private'; </script> <p>API Key: {PRIVATE_API_KEY}</p>
Remember that $env/static/private variables are replaced at build time.
The PRIVATE_API_KEY is statically injected during build, so the component renders the actual value abc123.
You want to use a public environment variable named VITE_API_URL in your SvelteKit component. Which import statement is correct?
Public variables start with VITE_ and are imported from $env/static/public.
Public environment variables prefixed with VITE_ are accessed via $env/static/public for static imports.
Given this code snippet:
<script>
import { SECRET_KEY } from '$env/dynamic/private';
console.log(SECRET_KEY);
</script>At runtime, the console shows undefined for SECRET_KEY. What is the most likely cause?
import { SECRET_KEY } from '$env/dynamic/private'; console.log(SECRET_KEY);
Dynamic private variables reflect the environment at runtime, not build time.
If SECRET_KEY is undefined at runtime, it means the environment variable is not set in the running environment. Dynamic imports read the actual environment variables when the app runs.
Consider this Svelte component:
<script>
import { env } from '$env/dynamic/public';
</script>
<p>API URL: {env.VITE_API_URL}</p>If the environment variable VITE_API_URL is set to https://api.example.com at runtime, what will the component display?
<script> import { env } from '$env/dynamic/public'; </script> <p>API URL: {env.VITE_API_URL}</p>
Dynamic public env variables reflect the environment at runtime.
The env object from $env/dynamic/public contains public variables available at runtime, so the component shows the actual URL.
Choose the correct statement about environment variables in SvelteKit:
Think about build time vs runtime and public vs private variables.
Static private variables are replaced at build time and never exposed to client code. Public variables start with 'VITE_' and are accessible in client code. Dynamic env variables are accessed via the 'env' object, not named imports.