Challenge - 5 Problems
Dotenv Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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);
Attempts:
2 left
💡 Hint
Remember dotenv loads variables from .env into process.env when config() is called.
✗ Incorrect
dotenv.config() reads the .env file and sets process.env variables. So console.log prints the APP_NAME value if defined.
❓ component_behavior
intermediate2: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?
Attempts:
2 left
💡 Hint
Check dotenv.config() return value when .env is missing.
✗ Incorrect
dotenv.config() returns an object with an error property if .env is missing, but does not throw or stop the app.
📝 Syntax
advanced2: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?
Attempts:
2 left
💡 Hint
Remember ES modules use import syntax and dotenv.config() is the correct method.
✗ Incorrect
Options B and C both use ES module import syntax and call config(), which is correct for ES modules.
🔧 Debug
advanced2: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?
Attempts:
2 left
💡 Hint
Think about when dotenv loads variables relative to when you read them.
✗ Incorrect
dotenv.config() loads variables into process.env. Accessing process.env before config() runs shows undefined.
🧠 Conceptual
expert3: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?
Attempts:
2 left
💡 Hint
dotenv.config() accepts a path option to specify which file to load.
✗ Incorrect
dotenv does not auto-detect environment files. You must specify the path manually based on environment.