Challenge - 5 Problems
Environment Configuration Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate1:30remaining
What is the purpose of the .env file in a Node.js project?
Choose the best explanation for why developers use a .env file in Node.js projects.
Attempts:
2 left
💡 Hint
Think about how you keep secret keys or settings separate from your code.
✗ Incorrect
The .env file holds environment variables like API keys or database URLs. This keeps sensitive info out of the code and allows easy changes per environment.
❓ component_behavior
intermediate1:30remaining
What will this Node.js code output when using dotenv?
Given the .env file contains: API_KEY=12345
What does the following code print?
Node.js
import dotenv from 'dotenv'; dotenv.config(); console.log(process.env.API_KEY);
Attempts:
2 left
💡 Hint
dotenv loads variables into process.env
✗ Incorrect
dotenv.config() loads variables from .env into process.env. So process.env.API_KEY will be '12345'.
📝 Syntax
advanced2:00remaining
Which option correctly loads environment variables using ES modules in Node.js?
Select the code snippet that correctly imports and configures dotenv in a Node.js ES module.
Attempts:
2 left
💡 Hint
ES modules use import syntax, not require.
✗ Incorrect
In ES modules, you can import the named export 'config' and call it directly. Option A uses correct ES module syntax.
🔧 Debug
advanced2:00remaining
Why does process.env.MY_VAR return undefined in this Node.js app?
Given .env contains MY_VAR=hello
And code is:
import dotenv from 'dotenv';
console.log(process.env.MY_VAR);
dotenv.config();
Why is the output undefined?
Node.js
import dotenv from 'dotenv'; console.log(process.env.MY_VAR); dotenv.config();
Attempts:
2 left
💡 Hint
Think about when dotenv loads the variables.
✗ Incorrect
dotenv.config() loads variables into process.env. If you access process.env before calling config(), variables are not loaded yet.
❓ state_output
expert2:30remaining
What is the output of this Node.js code with multiple dotenv.config calls?
Assuming .env contains VAR=first and .env.local contains VAR=second,
What does this code print?
Node.js
import dotenv from 'dotenv'; dotenv.config({ path: '.env' }); dotenv.config({ path: '.env.local' }); console.log(process.env.VAR);
Attempts:
2 left
💡 Hint
dotenv.config() does not override existing environment variables by default.
✗ Incorrect
dotenv.config() loads variables into process.env but does not overwrite existing keys by default (override: false). The first call sets VAR='first'. The second call finds VAR already set and skips it.