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?
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}`);
Remember the nullish coalescing operator ?? sets a default if the left side is null or undefined.
If PORT is not set, Deno.env.get("PORT") returns undefined, so envPort becomes "3000". The JSON file contains {"mode": "production"}, so config.mode is "production".
You want to load environment variables from a .env file using Deno's standard library. Which code snippet is correct?
Check the official Deno standard library documentation for dotenv usage and import paths.
Option C uses the correct import path with versioning and the correct function config() which returns a promise resolving to environment variables.
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?
const configText = await Deno.readTextFile("./config.json"); const config = JSON.parse(configText); console.log(config);
Think about Deno's security model and how it restricts file system access.
Deno requires explicit permission flags to access the file system. Without --allow-read, reading files causes a PermissionDenied error.
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?
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);
Remember environment variables are strings. The nullish coalescing operator picks the first defined value.
The environment variable PORT is set to string "5000", so envPort is "5000" (string). The config port from file is ignored because envPort is not null or undefined. Mode comes from fileConfig as "production".
Choose the statement that best explains how Deno encourages managing configuration in applications.
Think about Deno's security model and how it handles environment variables and file access.
Deno uses explicit permission flags for file and environment access, encouraging developers to load configuration securely and flexibly, often from environment variables or files with explicit code.
