Bird
Raised Fist0
Node.jsframework~20 mins

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

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.

Practice

(1/5)
1. What is the main purpose of using dotenv in a Node.js project?
easy
A. To manage database connections automatically
B. To compile JavaScript code faster
C. To load environment variables from a file into process.env
D. To create HTTP servers easily

Solution

  1. Step 1: Understand what dotenv does

    dotenv reads a file (usually .env) and loads variables into process.env.
  2. Step 2: Identify the main purpose

    This allows your app to access secret or environment-specific settings safely without hardcoding them.
  3. Final Answer:

    To load environment variables from a file into process.env -> Option C
  4. Quick Check:

    dotenv loads env vars = C [OK]
Hint: dotenv loads .env variables into process.env [OK]
Common Mistakes:
  • Thinking dotenv compiles or runs code
  • Confusing dotenv with database or server tools
  • Expecting dotenv to manage app logic
2. Which of the following is the correct way to load environment variables using dotenv in a Node.js file?
easy
A. require('dotenv').config()
B. import dotenv from 'dotenv'; dotenv.load()
C. dotenv.setup()
D. require('dotenv').loadEnv()

Solution

  1. Step 1: Recall the dotenv usage syntax

    The official and common way to load variables is calling require('dotenv').config() at the start of your app.
  2. Step 2: Check other options for correctness

    The other options use incorrect method names or syntax not supported by dotenv.
  3. Final Answer:

    require('dotenv').config() -> Option A
  4. Quick Check:

    Use config() method to load dotenv [OK]
Hint: Use require('dotenv').config() to load env vars [OK]
Common Mistakes:
  • Using wrong method names like load or setup
  • Forgetting to call config() function
  • Trying to import dotenv without config call
3. Given the following code and .env file, what will be the output?

// .env file content
API_KEY=12345
PORT=8080


require('dotenv').config();
console.log(process.env.API_KEY);
console.log(process.env.PORT);
medium
A. undefined\nundefined
B. null\nnull
C. API_KEY\nPORT
D. 12345\n8080

Solution

  1. Step 1: dotenv loads variables from .env into process.env

    After calling require('dotenv').config(), process.env.API_KEY is set to "12345" and process.env.PORT is set to "8080" as strings.
  2. Step 2: console.log prints the values

    The console will output the values exactly as strings, separated by new lines.
  3. Final Answer:

    12345 8080 -> Option D
  4. Quick Check:

    dotenv loads vars as strings = 12345\n8080 [OK]
Hint: dotenv sets process.env vars as strings from .env [OK]
Common Mistakes:
  • Expecting numbers instead of strings
  • Not calling config() before accessing vars
  • Assuming variables are undefined without loading dotenv
4. What is the error in the following code snippet that prevents environment variables from loading correctly?

const dotenv = require('dotenv');
dotenv.config;
console.log(process.env.SECRET_KEY);
medium
A. Missing parentheses after config function call
B. dotenv package not installed
C. SECRET_KEY not defined in .env file
D. Using require instead of import

Solution

  1. Step 1: Check how dotenv.config is called

    The code uses dotenv.config; without parentheses, so the function is not executed.
  2. Step 2: Understand the effect of missing parentheses

    Without calling config(), environment variables are not loaded into process.env, so SECRET_KEY remains undefined.
  3. Final Answer:

    Missing parentheses after config function call -> Option A
  4. Quick Check:

    Call config() with () to load env vars [OK]
Hint: Always call config() with parentheses to load env [OK]
Common Mistakes:
  • Forgetting parentheses on config function
  • Assuming require auto-executes config
  • Ignoring missing .env file or variable
5. You want to use dotenv to load different environment variables for development and production. Your .env file has NODE_ENV=development and API_URL=http://localhost:3000. You also have a .env.production file with NODE_ENV=production and API_URL=https://api.example.com. How can you load the correct file based on the environment?
hard
A. Use dotenv.loadEnv(process.env.NODE_ENV) to auto-load
B. Call require('dotenv').config({ path: `.env.${process.env.NODE_ENV}` }) after setting NODE_ENV
C. Rename .env.production to .env manually before running
D. Call require('dotenv').config() only once without options

Solution

  1. Step 1: Understand dotenv supports custom paths

    dotenv's config function accepts a path option to specify which file to load.
  2. Step 2: Use NODE_ENV to select the file dynamically

    By calling require('dotenv').config({ path: `.env.${process.env.NODE_ENV}` }), you load .env.development or .env.production based on the environment.
  3. Final Answer:

    Call require('dotenv').config({ path: `.env.${process.env.NODE_ENV}` }) after setting NODE_ENV -> Option B
  4. Quick Check:

    Use config({ path }) to load env files by environment [OK]
Hint: Use config({ path: `.env.${NODE_ENV}` }) to load env files [OK]
Common Mistakes:
  • Not specifying path option for different env files
  • Manually renaming files instead of dynamic loading
  • Using nonexistent dotenv.loadEnv method