0
0
Svelteframework~20 mins

Environment variables ($env) in Svelte - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Environment Variables Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this Svelte component using $env/static/private?

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?

Svelte
<script>
  import { PRIVATE_API_KEY } from '$env/static/private';
</script>

<p>API Key: {PRIVATE_API_KEY}</p>
AAPI Key: undefined
BAPI Key: $env/static/private
CAPI Key: abc123
DAPI Key: null
Attempts:
2 left
💡 Hint

Remember that $env/static/private variables are replaced at build time.

📝 Syntax
intermediate
2:00remaining
Which import statement correctly accesses a public environment variable in SvelteKit?

You want to use a public environment variable named VITE_API_URL in your SvelteKit component. Which import statement is correct?

Aimport { VITE_API_URL } from '$env/dynamic/private';
Bimport { VITE_API_URL } from '$env/static/private';
Cimport { VITE_API_URL } from '$env/dynamic/public';
Dimport { VITE_API_URL } from '$env/static/public';
Attempts:
2 left
💡 Hint

Public variables start with VITE_ and are imported from $env/static/public.

🔧 Debug
advanced
2:00remaining
Why does this SvelteKit code throw an error at runtime?

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?

Svelte
import { SECRET_KEY } from '$env/dynamic/private';
console.log(SECRET_KEY);
ASECRET_KEY is not set in the environment at runtime.
BYou must import from '$env/static/private' instead.
CSECRET_KEY must start with VITE_ to be accessible.
DYou cannot use environment variables in SvelteKit.
Attempts:
2 left
💡 Hint

Dynamic private variables reflect the environment at runtime, not build time.

state_output
advanced
2:00remaining
What is the output of this Svelte component using $env/dynamic/public?

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?

Svelte
<script>
  import { env } from '$env/dynamic/public';
</script>

<p>API URL: {env.VITE_API_URL}</p>
AAPI URL: https://api.example.com
BAPI URL: undefined
CAPI URL: $env/dynamic/public
DAPI URL: null
Attempts:
2 left
💡 Hint

Dynamic public env variables reflect the environment at runtime.

🧠 Conceptual
expert
2:00remaining
Which statement about SvelteKit environment variables is TRUE?

Choose the correct statement about environment variables in SvelteKit:

AVariables from '$env/static/public' are only available at runtime on the server.
BVariables imported from '$env/static/private' are replaced at build time and not exposed to client-side code.
CDynamic environment variables can be imported using named imports from '$env/dynamic/private'.
DPublic environment variables must start with 'PRIVATE_' to be accessible in client code.
Attempts:
2 left
💡 Hint

Think about build time vs runtime and public vs private variables.