📖 Scenario: You are building a simple Svelte app that shows a greeting message. The greeting text should come from an environment variable to keep it flexible and secure.
🎯 Goal: Create a Svelte component that reads a greeting message from an environment variable and displays it on the page.
📋 What You'll Learn
Create an environment variable named PUBLIC_GREETING with the value Hello from environment!
Import the environment variable using $env/static/public in the Svelte component
Display the greeting message inside an <h1> tag
Ensure the component updates correctly when the environment variable changes
💡 Why This Matters
🌍 Real World
Environment variables are used to store configuration like API keys, URLs, or messages without hardcoding them in your app. This keeps your app flexible and secure.
💼 Career
Knowing how to use environment variables is essential for frontend developers working with frameworks like Svelte, React, or Vue, especially when deploying apps to different environments.
Progress0 / 4 steps
1
Set up the environment variable
Create a file named .env in your project root. Add the line PUBLIC_GREETING='Hello from environment!' exactly as shown.
Svelte
Hint
The environment variable must start with PUBLIC_ to be accessible in Svelte.
2
Import the environment variable in the Svelte component
In src/App.svelte, import PUBLIC_GREETING from $env/static/public using import { PUBLIC_GREETING } from '$env/static/public';.
Svelte
Hint
Use $env/static/public to import public environment variables starting with PUBLIC_.
3
Display the greeting message in the component
Add an <h1> tag inside src/App.svelte that displays the imported PUBLIC_GREETING variable using curly braces: <h1>{PUBLIC_GREETING}</h1>.
Svelte
Hint
Use curly braces {} to insert JavaScript variables inside Svelte markup.
4
Complete the Svelte component
Ensure the src/App.svelte file exports a default component with the import and the <h1> tag showing PUBLIC_GREETING. The file should start with the import and then the markup.
Svelte
Hint
The component is complete when it imports the variable and displays it inside an <h1> tag.
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.