What are environment variables in the context of deploying a Deno application?
Environment variables are key-value pairs set outside the application code that store configuration data like API keys or database URLs. They help keep sensitive info safe and allow easy changes without modifying code.
Click to reveal answer
beginner
How do you access environment variables in a Deno application?
Use the global Deno.env.get('VARIABLE_NAME') function to read environment variables safely during runtime.
Click to reveal answer
beginner
Why should you avoid hardcoding sensitive data like API keys directly in your Deno app code?
Hardcoding sensitive data risks exposing it publicly if code is shared or pushed to repositories. Using environment variables keeps secrets outside the code and safer during deployment.
Click to reveal answer
intermediate
What is a common way to set environment variables when deploying a Deno app on a cloud platform?
Most cloud platforms let you define environment variables in their dashboard or CLI. These variables are then available to your app at runtime without changing your code.
Click to reveal answer
intermediate
How can you securely pass environment variables to a Deno app when running locally?
You can create a .env file with variables and use a library like deno dotenv to load them, or set them directly in your terminal before running the app.
Click to reveal answer
Which Deno function reads an environment variable?
ADeno.env.load()
BDeno.env.set()
CDeno.env.get()
DDeno.env.read()
✗ Incorrect
Use Deno.env.get('VARIABLE_NAME') to read environment variables.
Why use environment variables instead of hardcoding secrets?
ATo make code run faster
BTo make the app smaller
CTo avoid using any configuration
DTo keep secrets safe and separate from code
✗ Incorrect
Environment variables keep sensitive data safe and separate from code.
Where do you usually set environment variables for a deployed Deno app?
AIn the cloud platform's dashboard or CLI
BInside the app's source code
CIn the browser console
DIn the app's HTML files
✗ Incorrect
Cloud platforms provide ways to set environment variables outside the code.
What file is commonly used to store environment variables locally?
Aconfig.json
B.env
Cvariables.js
Dsettings.yaml
✗ Incorrect
The .env file is a standard way to store environment variables locally.
Which command sets an environment variable in a Unix terminal before running a Deno app?
Aexport API_KEY=123; deno run app.ts
Bset API_KEY=123 deno run app.ts
CAPI_KEY='123' deno run app.ts
Denv API_KEY=123 deno run app.ts
✗ Incorrect
Use export API_KEY=123; deno run app.ts to set environment variables in Unix shells.
Explain how environment variables improve security and flexibility when deploying a Deno application.
Think about why you wouldn't want to put passwords directly in your code.
You got /4 concepts.
Describe the steps to access and use environment variables in a Deno app running locally and in production.
Consider both local development and deployment environments.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of environment variables when deploying a Deno application?
easy
A. To keep secrets and settings outside the code for security and flexibility
B. To write all configuration directly inside the source code
C. To make the application run faster by hardcoding values
D. To store user data permanently on the server
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 A
Hint: Environment variables hold secrets outside code [OK]
Common Mistakes:
Thinking environment variables are for storing user data
Hardcoding secrets inside source code
Believing environment variables speed up code execution
2. Which of the following is the correct way to read an environment variable named API_KEY in a Deno script?
easy
A. const key = getenv("API_KEY");
B. const key = process.env.API_KEY;
C. const key = Deno.env.get("API_KEY");
D. const key = env.API_KEY;
Solution
Step 1: Recall Deno environment variable syntax
Deno uses Deno.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 C
Quick Check:
Deno.env.get("API_KEY") = correct syntax [OK]
Hint: Use Deno.env.get("VAR_NAME") to read env variables [OK]
Common Mistakes:
Using Node.js syntax like process.env in Deno
Calling undefined functions like getenv
Accessing env variables without Deno.env.get
3. Given the following Deno code snippet deployed with environment variable PORT=8080, what will be the output?
const port = Deno.env.get("PORT") || "3000";
console.log(`Server running on port ${port}`);
medium
A. Server running on port 3000
B. Server running on port 8080
C. Server running on port undefined
D. Error: PORT is not defined
Solution
Step 1: Check environment variable value
The environment variable PORT is set to "8080" in deployment.
Step 2: Evaluate fallback logic
The code uses Deno.env.get("PORT") || "3000", so it uses "8080" because PORT exists.
Final Answer:
Server running on port 8080 -> Option B
Quick Check:
PORT=8080 overrides default 3000 [OK]
Hint: Env var set? Use it; else fallback value [OK]
Common Mistakes:
Assuming fallback value always prints
Expecting error if env var exists
Confusing undefined with fallback
4. You deployed a Deno app but reading environment variables returns undefined. Which fix will solve this problem?
medium
A. Restart the app without setting variables
B. Change Deno.env.get to process.env
C. Hardcode variables inside the source code
D. Set environment variables in the deployment platform settings
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 ensures Deno.env.get can read them.
Final Answer:
Set environment variables in the deployment platform settings -> Option D
Quick Check:
Env vars must be set in deploy settings [OK]
Hint: Always set env vars in deployment platform [OK]
Common Mistakes:
Using Node.js syntax in Deno
Hardcoding secrets instead of setting env vars
Restarting app without setting variables
5. You want to deploy a Deno app that uses an environment variable MODE to switch between 'development' and 'production'. Which approach correctly uses environment variables to change app behavior safely?
hard
A. Set MODE in deployment settings and use Deno.env.get("MODE") in code to conditionally run features
B. Hardcode const MODE = "production" in source code and redeploy to change
C. Use a config file checked into source with MODE value
D. Ask users to input MODE at runtime every time
Solution
Step 1: Understand environment variable usage for modes
Environment variables allow switching modes without changing code.
Step 2: Choose safe and flexible method
Setting MODE in deployment and reading it with Deno.env.get enables safe conditional logic.
Final Answer:
Set MODE in deployment settings and use Deno.env.get("MODE") in code to conditionally run features -> Option A
Quick Check:
Env vars control modes safely without code changes [OK]
Hint: Use env vars to toggle modes, not hardcoding [OK]
Common Mistakes:
Hardcoding mode and redeploying for changes
Storing secrets or modes in config files checked into source
Asking users to input mode every run
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.