Environment variables are special values set outside your code that your app can use to store settings like API keys or mode (development or production). They help keep sensitive info safe and make your app flexible.
Click to reveal answer
intermediate
How do you access environment variables in SvelteKit using $env?
You import <code>$env/static/private</code> or <code>$env/static/public</code> to access variables at build time, or <code>$env/dynamic/private</code> and <code>$env/dynamic/public</code> for runtime access. Public variables start with <code>PUBLIC_</code> and are safe to expose to the browser.
Click to reveal answer
beginner
Why should environment variables starting with PUBLIC_ be used carefully?
Variables starting with PUBLIC_ are exposed to the browser, so they should not contain secrets like passwords or private keys. Use them only for safe, public info like API URLs.
Click to reveal answer
intermediate
What is the difference between static and dynamic environment variables in SvelteKit?
Static variables are replaced at build time and cannot change after. Dynamic variables are read at runtime, allowing changes without rebuilding the app.
Click to reveal answer
beginner
How do you add environment variables to your SvelteKit project?
Create a .env file in your project root and add variables like API_KEY=12345. Then restart the dev server to load them. Use PUBLIC_ prefix for variables you want available in the browser.
Click to reveal answer
Which prefix should you use for environment variables that need to be accessible in the browser in SvelteKit?
APRIVATE_
BPUBLIC_
CSECRET_
DCLIENT_
✗ Incorrect
Only variables starting with PUBLIC_ are exposed to the browser in SvelteKit.
Where do you import static environment variables from in SvelteKit?
Aprocess.env
B$env/dynamic/private or $env/dynamic/public
Cimport.meta.env
D$env/static/private or $env/static/public
✗ Incorrect
Static environment variables are imported from $env/static/private or $env/static/public in SvelteKit.
What happens if you change a static environment variable after building your SvelteKit app?
AThe variable is ignored
BThe app updates automatically
CThe app must be rebuilt to see the change
DThe app crashes
✗ Incorrect
Static variables are baked into the build, so you must rebuild the app to apply changes.
Which file do you typically use to define environment variables in a SvelteKit project?
A.env
Bconfig.js
Cpackage.json
Dindex.html
✗ Incorrect
Environment variables are usually defined in a .env file at the project root.
Why should secret keys NOT be prefixed with PUBLIC_ in SvelteKit?
ABecause PUBLIC_ variables are exposed to the browser
BBecause PUBLIC_ variables are ignored
CBecause PUBLIC_ variables are encrypted
DBecause PUBLIC_ variables are only for server use
✗ Incorrect
PUBLIC_ variables are sent to the browser, so secrets should never use this prefix.
Explain how to use environment variables in a SvelteKit app, including how to keep secrets safe.
Think about build time vs runtime and public vs private variables.
You got /5 concepts.
Describe the difference between static and dynamic environment variables in SvelteKit and when to use each.
Consider when your app needs to read the variable.
You got /5 concepts.
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
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.
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.
Final Answer:
To store settings outside the code for safety and flexibility -> Option D
Quick Check:
Environment variables = safety and flexibility [OK]
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
Step 1: Understand static vs dynamic public env variables
Static public variables are fixed at build time, so changes require rebuilding the app.
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.
Final Answer:
Import PUBLIC_GREETING from $env/dynamic/public and use it in your component -> Option B
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 unknownstrongwhile earning +1 Grip toward your certificate.