How to make VS Code read dotenv file when debugging

When VS Code launches a Node program for debugging, it doesn't pick up the variables from your project's .env file. The instinct is to wedge -r dotenv/config into the launch command, or to require('dotenv').config() at the top of the entry file. Both work, but they mutate argv or require app-code changes you don't want shipping past the debug session, and neither generalises to non-Node debug types.

The right lever is the envFile attribute in launch.json. VS Code reads the file before the process starts, injects the variables into the launched environment, and leaves your code alone. It works for any debug type that supports it.

Minimal Node launch config:

1{ 2 "version": "0.2.0", 3 "configurations": [ 4 { 5 "type": "node", 6 "request": "launch", 7 "name": "Launch Program", 8 "program": "${workspaceFolder}/src/app.js", 9 "envFile": "${workspaceFolder}/.env", 10 "skipFiles": ["<node_internals>/**"] 11 } 12 ] 13}

"type": "node" is the modern built-in JS debugger; the older "node2" type was retired years ago. See the VS Code debugging docs for the full set of attributes.