Bird
Raised Fist0
Denoframework~20 mins

Configuration management in Deno - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Deno Configuration Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What is the output of this Deno configuration loading code?

Consider this Deno script that loads configuration from environment variables and a JSON file.

import { readJson } from "https://deno.land/std/fs/mod.ts";

const envPort = Deno.env.get("PORT") ?? "3000";
const config = await readJson("./config.json");

console.log(`Port: ${envPort}, Mode: ${config.mode}`);

If the environment variable PORT is not set and config.json contains {"mode": "production"}, what will be printed?

Deno
import { readJson } from "https://deno.land/std/fs/mod.ts";

const envPort = Deno.env.get("PORT") ?? "3000";
const config = await readJson("./config.json");

console.log(`Port: ${envPort}, Mode: ${config.mode}`);
APort: 3000, Mode: undefined
BPort: undefined, Mode: production
CPort: undefined, Mode: undefined
DPort: 3000, Mode: production
Attempts:
2 left
💡 Hint

Remember the nullish coalescing operator ?? sets a default if the left side is null or undefined.

📝 Syntax
intermediate
2:00remaining
Which option correctly imports and uses Deno's standard config module to load a .env file?

You want to load environment variables from a .env file using Deno's standard library. Which code snippet is correct?

A
import { dotenv } from "https://deno.land/std/dotenv/mod.ts";
const env = dotenv.load();
console.log(env.MY_VAR);
B
import { loadEnv } from "https://deno.land/std/dotenv/mod.ts";
const env = loadEnv();
console.log(env.MY_VAR);
C
import { config } from "https://deno.land/std@0.177.0/dotenv/mod.ts";
const env = await config();
console.log(env.MY_VAR);
D
import { config } from "https://deno.land/std/dotenv.ts";
const env = await config();
console.log(env.MY_VAR);
Attempts:
2 left
💡 Hint

Check the official Deno standard library documentation for dotenv usage and import paths.

🔧 Debug
advanced
2:00remaining
Why does this Deno config loading code throw a PermissionDenied error?

Given this Deno script:

const configText = await Deno.readTextFile("./config.json");
const config = JSON.parse(configText);
console.log(config);

When running deno run script.ts, it throws a PermissionDenied error. Why?

Deno
const configText = await Deno.readTextFile("./config.json");
const config = JSON.parse(configText);
console.log(config);
ADeno requires explicit permission to read files; the script was run without the --allow-read flag.
BThe config.json file is missing or corrupted, causing a read failure.
CJSON.parse throws PermissionDenied if the JSON is invalid.
DDeno scripts cannot read local files by default due to sandboxing in the runtime.
Attempts:
2 left
💡 Hint

Think about Deno's security model and how it restricts file system access.

❓ state_output
advanced
2:00remaining
What is the value of config after merging environment variables and JSON config?

Consider this Deno code snippet:

import { readJson } from "https://deno.land/std/fs/mod.ts";

const envPort = Deno.env.get("PORT");
const fileConfig = await readJson("./config.json");

const config = {
  port: envPort ?? fileConfig.port ?? 8080,
  mode: fileConfig.mode ?? "development"
};

console.log(config);

If PORT is set to "5000" in environment, and config.json contains {"port": 3000, "mode": "production"}, what will config be?

Deno
import { readJson } from "https://deno.land/std/fs/mod.ts";

const envPort = Deno.env.get("PORT");
const fileConfig = await readJson("./config.json");

const config = {
  port: envPort ?? fileConfig.port ?? 8080,
  mode: fileConfig.mode ?? "development"
};

console.log(config);
A{"port": "5000", "mode": "production"}
B{"port": 5000, "mode": "production"}
C{"port": 3000, "mode": "production"}
D{"port": 8080, "mode": "development"}
Attempts:
2 left
💡 Hint

Remember environment variables are strings. The nullish coalescing operator picks the first defined value.

🧠 Conceptual
expert
2:00remaining
Which statement best describes Deno's approach to configuration management?

Choose the statement that best explains how Deno encourages managing configuration in applications.

ADeno enforces configuration through a centralized config file format and built-in parser for all apps.
BDeno encourages explicit permission flags and environment variables, promoting secure and flexible configuration loading.
CDeno requires all configuration to be embedded in source code to avoid runtime errors.
DDeno automatically loads .env files without any additional code or permissions.
Attempts:
2 left
💡 Hint

Think about Deno's security model and how it handles environment variables and file access.

Practice

(1/5)
1. What is the main purpose of configuration management in a Deno application?
easy
A. To separate settings like API keys from the main code for flexibility and security
B. To write all code and settings in a single file for simplicity
C. To hardcode secret keys directly in the source code
D. To avoid using environment variables altogether

Solution

  1. Step 1: Understand configuration management role

    Configuration management separates settings from code to keep apps flexible and secure.
  2. Step 2: Identify correct practice in Deno

    Using environment variables or .env files keeps secrets out of code and allows easy changes.
  3. Final Answer:

    To separate settings like API keys from the main code for flexibility and security -> Option A
  4. Quick Check:

    Config management = separate settings [OK]
Hint: Remember: keep secrets out of code, use config files [OK]
Common Mistakes:
  • Hardcoding secrets in source code
  • Mixing code and settings in one file
  • Ignoring environment-specific configs
2. Which of the following is the correct way to load environment variables from a .env file in Deno?
easy
A. require('dotenv').config();
B. import { config } from "https://deno.land/std/dotenv/mod.ts"; const env = config();
C. import dotenv from "dotenv"; dotenv.load();
D. import { loadEnv } from "deno_env"; loadEnv();

Solution

  1. Step 1: Identify Deno's dotenv import syntax

    Deno uses ES module imports from URLs, like importing config from std/dotenv.
  2. Step 2: Check correct usage

    Calling config() loads environment variables from .env file correctly in Deno.
  3. Final Answer:

    import { config } from "https://deno.land/std/dotenv/mod.ts"; const env = config(); -> Option B
  4. Quick Check:

    Deno dotenv import = import { config } from "https://deno.land/std/dotenv/mod.ts"; const env = config(); [OK]
Hint: Use Deno's std/dotenv with ES module import [OK]
Common Mistakes:
  • Using Node.js require syntax in Deno
  • Using non-existent packages
  • Calling wrong functions to load env
3. Given this Deno code snippet:
import { config } from "https://deno.land/std/dotenv/mod.ts";
const env = config();
console.log(env.API_KEY);

If the .env file contains API_KEY=12345, what will be printed?
medium
A. undefined
B. Error: .env file not found
C. API_KEY
D. 12345

Solution

  1. Step 1: Understand config() output

    The config() function reads .env and returns an object with keys and values.
  2. Step 2: Access API_KEY value

    env.API_KEY accesses the value '12345' from the loaded environment variables.
  3. Final Answer:

    12345 -> Option D
  4. Quick Check:

    env.API_KEY = '12345' [OK]
Hint: config() returns env vars as object keys [OK]
Common Mistakes:
  • Expecting config() to print keys directly
  • Forgetting to create .env file
  • Assuming env variables are undefined
4. What is wrong with this Deno code snippet for loading environment variables?
import { config } from "https://deno.land/std/dotenv/mod.ts";
const env = config;
console.log(env.API_SECRET);
medium
A. config is not imported correctly
B. API_SECRET is not a valid environment variable name
C. config is assigned without calling it as a function
D. console.log syntax is incorrect

Solution

  1. Step 1: Check how config is used

    config is a function and must be called with parentheses to load env variables.
  2. Step 2: Identify the error

    Assigning config without () means env is the function itself, not the loaded object.
  3. Final Answer:

    config is assigned without calling it as a function -> Option C
  4. Quick Check:

    Call config() to load env [OK]
Hint: Always call config() with parentheses [OK]
Common Mistakes:
  • Assigning function reference instead of calling it
  • Misspelling config import
  • Using wrong variable names
5. You want to manage different configurations for development and production in a Deno app. Which approach is best practice?
hard
A. Use separate .env files like .env.development and .env.production and load the correct one based on an environment variable
B. Hardcode production secrets in the code and use .env only for development
C. Use a single .env file with all keys and comment out unused ones manually
D. Store all secrets in a JSON file checked into version control

Solution

  1. Step 1: Understand environment-specific configs

    Different environments need different settings to avoid mistakes and leaks.
  2. Step 2: Choose best practice for Deno

    Using separate .env files and loading based on an environment variable keeps configs clean and secure.
  3. Final Answer:

    Use separate .env files like .env.development and .env.production and load the correct one based on an environment variable -> Option A
  4. Quick Check:

    Separate env files per environment = Use separate .env files like .env.development and .env.production and load the correct one based on an environment variable [OK]
Hint: Use multiple .env files and switch by env variable [OK]
Common Mistakes:
  • Hardcoding secrets in code
  • Using one .env for all environments
  • Checking secrets into version control
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard