0
0
Node.jsframework~20 mins

Environment configuration in Node.js - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Environment Configuration Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1: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.
ATo define the structure of the database used by the application.
BTo write the main application logic and server code.
CTo list all installed Node.js packages and their versions.
DTo store environment-specific configuration variables securely outside the codebase.
Attempts:
2 left
💡 Hint
Think about how you keep secret keys or settings separate from your code.
component_behavior
intermediate
1: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);
AError: dotenv not found
Bundefined
C12345
Dnull
Attempts:
2 left
💡 Hint
dotenv loads variables into process.env
📝 Syntax
advanced
2: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.
A
import { config } from 'dotenv';
config();
B
const dotenv = require('dotenv');
dotenv.config();
Crequire('dotenv').config();
D
import dotenv from 'dotenv';
dotenv.config();
Attempts:
2 left
💡 Hint
ES modules use import syntax, not require.
🔧 Debug
advanced
2: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();
Aprocess.env variables are only available in CommonJS modules.
Bdotenv.config() must be called before accessing process.env variables.
CThe .env file is missing from the project root.
DMY_VAR is not a valid environment variable name.
Attempts:
2 left
💡 Hint
Think about when dotenv loads the variables.
state_output
expert
2: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);
Afirst
BError: Multiple dotenv.config calls not allowed
Cundefined
Dsecond
Attempts:
2 left
💡 Hint
dotenv.config() does not override existing environment variables by default.