Bird
Raised Fist0
Svelteframework~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main reason to use environment variables in a Svelte project?
easy
A. To avoid using any external libraries
B. To write all code inside a single file
C. To make the app run faster by skipping variables
D. To store settings outside the code for safety and flexibility

Solution

  1. Step 1: Understand environment variables purpose

    Environment variables keep sensitive or changeable settings outside the main code, so you can change them without editing code.
  2. Step 2: Connect to safety and flexibility

    This separation helps keep secrets safe and lets you adjust settings easily for different environments like development or production.
  3. Final Answer:

    To store settings outside the code for safety and flexibility -> Option D
  4. Quick Check:

    Environment variables = safety and flexibility [OK]
Hint: Environment variables keep secrets outside code [OK]
Common Mistakes:
  • Thinking environment variables speed up the app
  • Believing environment variables are for code organization only
  • Confusing environment variables with libraries
2. Which import statement correctly accesses public environment variables in a Svelte component?
easy
A. import { PUBLIC_API_KEY } from '$env/static/public';
B. import { API_KEY } from '$env/static/private';
C. import { API_KEY } from '$env/dynamic/private';
D. import { PUBLIC_API_KEY } from '$env/dynamic/public';

Solution

  1. Step 1: Identify public environment variable import

    Public variables are accessed from '$env/static/public' when known at build time.
  2. Step 2: Match variable prefix and import path

    Public variables must start with 'PUBLIC_' and be imported from '$env/static/public' for static access.
  3. Final Answer:

    import { PUBLIC_API_KEY } from '$env/static/public'; -> Option A
  4. Quick Check:

    Public vars use static/public import [OK]
Hint: Public vars use $env/static/public import [OK]
Common Mistakes:
  • Using private import paths for public variables
  • Forgetting the PUBLIC_ prefix
  • Mixing dynamic and static imports incorrectly
3. Given this Svelte code snippet, what will be displayed if the environment variable PUBLIC_APP_NAME is set to 'MyApp'?
import { PUBLIC_APP_NAME } from '$env/static/public';

<h1>Welcome to {PUBLIC_APP_NAME}!</h1>
medium
A.

Welcome to MyApp!

B.

Welcome to PUBLIC_APP_NAME!

C.

Welcome to !

D. Error: Cannot find variable PUBLIC_APP_NAME

Solution

  1. Step 1: Understand import of PUBLIC_APP_NAME

    The variable PUBLIC_APP_NAME is imported from '$env/static/public', so its value is available at build time.
  2. Step 2: Check usage in template

    The variable is used inside the <h1> tag, so it will display the actual value 'MyApp'.
  3. Final Answer:

    <h1>Welcome to MyApp!</h1> -> Option A
  4. Quick Check:

    Static public env var shows actual value [OK]
Hint: Static public vars show real value in template [OK]
Common Mistakes:
  • Expecting variable name instead of value
  • Assuming variable is undefined without import
  • Confusing static and dynamic imports
4. You try to access a private environment variable in a Svelte component using this code:
import { SECRET_KEY } from '$env/static/private';

console.log(SECRET_KEY);
But you get an error during build. What is the likely cause?
medium
A. SECRET_KEY must start with PUBLIC_ prefix
B. You must import from '$env/dynamic/public' instead
C. Private variables cannot be imported in client-side code
D. You forgot to declare SECRET_KEY in .env file

Solution

  1. Step 1: Understand private env variable usage

    Private environment variables are only available on the server side and cannot be imported into client-side code.
  2. Step 2: Recognize build error cause

    Importing private variables in a component that runs in the browser causes build errors because secrets must stay server-only.
  3. Final Answer:

    Private variables cannot be imported in client-side code -> Option C
  4. Quick Check:

    Private vars are server-only [OK]
Hint: Private vars only on server, not client [OK]
Common Mistakes:
  • Adding PUBLIC_ prefix to private vars
  • Using dynamic public import for private vars
  • Assuming missing .env declaration causes build error
5. You want to show a greeting message in your Svelte app that changes based on a public environment variable PUBLIC_GREETING. You also want to update this message without rebuilding the app. Which approach should you use?
hard
A. Import PUBLIC_GREETING from $env/static/public and use it directly
B. Import PUBLIC_GREETING from $env/dynamic/public and use it in your component
C. Store PUBLIC_GREETING in a local variable inside the component
D. Hardcode the greeting message in the component

Solution

  1. Step 1: Understand static vs dynamic public env variables

    Static public variables are fixed at build time, so changes require rebuilding the app.
  2. Step 2: Use dynamic public env for runtime updates

    Dynamic public variables from '$env/dynamic/public' reflect environment changes without rebuilding, ideal for updating messages.
  3. Final Answer:

    Import PUBLIC_GREETING from $env/dynamic/public and use it in your component -> Option B
  4. Quick Check:

    Dynamic public env allows runtime updates [OK]
Hint: Use $env/dynamic/public for runtime env changes [OK]
Common Mistakes:
  • Using static public import for dynamic needs
  • Storing env vars in local variables without import
  • Hardcoding messages instead of using env vars
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard