DEV Community

Andreas Bergström
Andreas Bergström

Posted on • Edited on

5 2

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.

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more

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"

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs