DEV Community

Andreas Bergström
Andreas Bergström

Posted on • Updated on

How to make VS Code read dotenv file when debugging

Today I got stuck trying to figure out why the JS-debugger in Visual Studio Code didn't pick up my environment variables. I use the dotenv-package when running Node outside of its Docker-container, so somehow adding -r dotenv/config to the command being executed seemed like the obvious solution.

It turns out the correct way is through the envFile attribute in the task configuration. This is how my task configuration looks now:

{
  "version": "0.2.0",
  "configurations": [
    {
      "type": "node",
      "stopOnEntry": false,
      "envFile": "${workspaceFolder}/.env",
      "request": "launch",
      "name": "Launch Program",
      "skipFiles": ["<node_internals>/**"],
      "program": "${workspaceFolder}/src/app.js"
    }
  ]
}

Enter fullscreen mode Exit fullscreen mode

I thought this should be mentioned with the most common examples in the docs so I created a Pull Request to add this.

Top comments (3)

Collapse
 
ramonmedeiros profile image
Ramon Medeiros

Thanks

Collapse
 
andreasbergstrom profile image
Andreas Bergström

Hope this still works!

Collapse
 
matthewbill profile image
Matthew Bill

If you are using a monorepo like me and getting this error. It might be because you have not set the working directory and so its not finding the correct file to load.

In this case you don't need to set the envFile, but can set the cwd and this means you can be sure when debugging your dotenv is working correctly as well:

"cwd": "${workspaceFolder}/apps/yourapp"