0
0
Svelteframework~5 mins

Environment variables ($env) in Svelte - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What are environment variables in Svelte?
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_
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
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
Which file do you typically use to define environment variables in a SvelteKit project?
A.env
Bconfig.js
Cpackage.json
Dindex.html
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
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.