Bird
0
0

You want to debug a Node.js application in VS Code that requires environment variables defined in a .env file. Which approach correctly loads these variables during debugging?

hard📝 Application Q8 of 15
Node.js - Debugging and Profiling
You want to debug a Node.js application in VS Code that requires environment variables defined in a .env file. Which approach correctly loads these variables during debugging?
AManually export variables in terminal before starting VS Code debugger
BUse the <code>dotenv</code> package in your code and configure <code>launch.json</code> to run the app normally
CAdd environment variables directly inside <code>launch.json</code> under the <code>env</code> property
DRename <code>.env</code> to <code>config.json</code> and import it in your code
Step-by-Step Solution
Solution:
  1. Step 1: Understand environment variable loading

    Node.js apps typically use the dotenv package to load variables from a .env file.
  2. Step 2: Configure launch.json

    The debugger runs the app normally; dotenv loads variables at runtime.
  3. Step 3: Other options

    Manually exporting variables (B) works but is less convenient; adding variables in launch.json (C) works but requires duplicating variables; renaming .env (D) is incorrect.
  4. Final Answer:

    Use the dotenv package in your code and configure launch.json to run the app normally -> Option B
  5. Quick Check:

    Use dotenv to load .env variables at runtime [OK]
Quick Trick: Use dotenv package to load .env variables during debug [OK]
Common Mistakes:
  • Assuming VS Code loads .env automatically
  • Manually adding variables in launch.json without code support
  • Renaming .env file incorrectly

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Node.js Quizzes