Using Environment Variables in Svelte
📖 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 componentDisplay the greeting message inside an
<h1> tagEnsure 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