Environment variables let you keep secret or changeable info outside your code. This helps keep your app safe and flexible when you deploy it.
Environment variables on Deploy in Deno
const value = Deno.env.get("VARIABLE_NAME");Use Deno.env.get to read environment variables in Deno.
Environment variables are strings or null if not set.
API_KEY environment variable and prints it.const apiKey = Deno.env.get("API_KEY");
console.log(apiKey);PORT or uses default 8000 if not set.const port = Deno.env.get("PORT") || "8000"; console.log(`Server runs on port ${port}`);
DATABASE_URL is set and warns if missing.if (!Deno.env.get("DATABASE_URL")) { console.error("Missing DATABASE_URL environment variable!"); }
This simple Deno server reads the PORT environment variable to decide which port to listen on. If PORT is not set, it uses 8080 by default. It then starts a server that responds with a greeting message.
import { serve } from "https://deno.land/std@0.201.0/http/server.ts"; const port = Deno.env.get("PORT") || "8080"; console.log(`Server running on http://localhost:${port}/`); serve((_req) => new Response("Hello from Deno!"), { port: +port });
Remember to set environment variables in your deployment platform settings or shell before running your Deno app.
Use deno run --allow-env to allow your program to access environment variables.
Never commit sensitive environment variables to public code repositories.
Environment variables keep secrets and settings outside your code.
Use Deno.env.get("NAME") to read them in Deno.
Set variables in your deployment environment to change app behavior safely.
Practice
Solution
Step 1: Understand environment variables role
Environment variables store secrets and settings outside the code to avoid exposing sensitive data.Step 2: Recognize deployment best practices
Keeping secrets outside code allows changing settings without modifying the app, improving security and flexibility.Final Answer:
To keep secrets and settings outside the code for security and flexibility -> Option AQuick Check:
Environment variables = external secrets/settings [OK]
- Thinking environment variables are for storing user data
- Hardcoding secrets inside source code
- Believing environment variables speed up code execution
API_KEY in a Deno script?Solution
Step 1: Recall Deno environment variable syntax
Deno usesDeno.env.get("NAME")to access environment variables.Step 2: Compare options
const key = Deno.env.get("API_KEY"); matches Deno syntax; others are from Node.js or incorrect.Final Answer:
const key = Deno.env.get("API_KEY"); -> Option CQuick Check:
Deno.env.get("API_KEY") = correct syntax [OK]
- Using Node.js syntax like process.env in Deno
- Calling undefined functions like getenv
- Accessing env variables without Deno.env.get
PORT=8080, what will be the output?const port = Deno.env.get("PORT") || "3000";
console.log(`Server running on port ${port}`);Solution
Step 1: Check environment variable value
The environment variable PORT is set to "8080" in deployment.Step 2: Evaluate fallback logic
The code usesDeno.env.get("PORT") || "3000", so it uses "8080" because PORT exists.Final Answer:
Server running on port 8080 -> Option BQuick Check:
PORT=8080 overrides default 3000 [OK]
- Assuming fallback value always prints
- Expecting error if env var exists
- Confusing undefined with fallback
undefined. Which fix will solve this problem?Solution
Step 1: Identify cause of undefined env variables
Undefined means variables are not set in the deployment environment.Step 2: Apply correct fix
Setting environment variables in deployment platform ensuresDeno.env.getcan read them.Final Answer:
Set environment variables in the deployment platform settings -> Option DQuick Check:
Env vars must be set in deploy settings [OK]
- Using Node.js syntax in Deno
- Hardcoding secrets instead of setting env vars
- Restarting app without setting variables
MODE to switch between 'development' and 'production'. Which approach correctly uses environment variables to change app behavior safely?Solution
Step 1: Understand environment variable usage for modes
Environment variables allow switching modes without changing code.Step 2: Choose safe and flexible method
SettingMODEin deployment and reading it withDeno.env.getenables safe conditional logic.Final Answer:
Set MODE in deployment settings and use Deno.env.get("MODE") in code to conditionally run features -> Option AQuick Check:
Env vars control modes safely without code changes [OK]
- Hardcoding mode and redeploying for changes
- Storing secrets or modes in config files checked into source
- Asking users to input mode every run
Ready to prove it?
Pass the test and this pattern moves unknown
