0
0
Node.jsframework~20 mins

dotenv for environment configuration in Node.js - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Dotenv Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this dotenv usage code?
Consider this Node.js code using dotenv to load environment variables. What will be logged to the console?
Node.js
import dotenv from 'dotenv';
dotenv.config();
console.log(process.env.APP_NAME);
AThe value of APP_NAME from the .env file
Bundefined
CThrows ReferenceError because dotenv is not imported correctly
DThrows SyntaxError due to missing dotenv config call
Attempts:
2 left
💡 Hint
Remember dotenv loads variables from .env into process.env when config() is called.
component_behavior
intermediate
2:00remaining
How does dotenv behave when .env file is missing?
If you run a Node.js app with dotenv but the .env file is missing, what happens?
Adotenv.config() returns an error and stops the app
Bdotenv.config() throws a runtime exception
Cdotenv.config() returns an object with an error property but app continues
Ddotenv.config() silently creates a new empty .env file
Attempts:
2 left
💡 Hint
Check dotenv.config() return value when .env is missing.
📝 Syntax
advanced
2:00remaining
Which option correctly loads dotenv in ES module syntax?
You want to use dotenv in a Node.js ES module. Which code snippet correctly loads and configures dotenv?
Aconst dotenv = require('dotenv'); dotenv.config();
Bimport dotenv from 'dotenv'; dotenv.config();
Cimport { config } from 'dotenv'; config();
Dimport * as dotenv from 'dotenv'; dotenv.load();
Attempts:
2 left
💡 Hint
Remember ES modules use import syntax and dotenv.config() is the correct method.
🔧 Debug
advanced
2:00remaining
Why does process.env.VAR remain undefined after dotenv.config()?
Given this code: import dotenv from 'dotenv'; console.log(process.env.MY_VAR); dotenv.config(); console.log(process.env.MY_VAR); MY_VAR is defined in .env. Why is the first console.log undefined?
Adotenv.config() must be called before accessing process.env variables
BMY_VAR is misspelled in .env file
CNode.js caches process.env before dotenv runs
Ddotenv.config() does not load variables automatically
Attempts:
2 left
💡 Hint
Think about when dotenv loads variables relative to when you read them.
🧠 Conceptual
expert
3:00remaining
What is the best practice for managing multiple environment files with dotenv?
You have different environment files like .env.development and .env.production. How should you load the correct one using dotenv?
Adotenv automatically detects and loads .env.production if NODE_ENV=production
BManually specify path in dotenv.config({ path: '.env.production' }) based on NODE_ENV
CRename the desired file to .env before running the app
DUse multiple dotenv.config() calls for each file
Attempts:
2 left
💡 Hint
dotenv.config() accepts a path option to specify which file to load.