The code uses dotenv.config; without parentheses, so the function is not executed.
Step 2: Understand the effect of missing parentheses
Without calling config(), environment variables are not loaded into process.env, so SECRET_KEY remains undefined.
Final Answer:
Missing parentheses after config function call -> Option A
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
Step 1: Understand dotenv supports custom paths
dotenv's config function accepts a path option to specify which file to load.
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.
Final Answer:
Call require('dotenv').config({ path: `.env.${process.env.NODE_ENV}` }) after setting NODE_ENV -> Option B
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