Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Andreas Bergström
Andreas Bergström

Posted on • Edited 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 thedotenv-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 theenvFile 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 modeExit fullscreen mode

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

Top comments(3)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
ramonmedeiros profile image
Ramon Medeiros
  • Joined

Thanks

CollapseExpand
 
andreasbergstrom profile image
Andreas Bergström
Started web dev before table layout was a thing, nowadays a full-stack web/mobile/backend/infra consultant.
  • Location
    Stockholm, Sweden
  • Education
    Computer Science
  • Work
    Full Stack Developer
  • Joined

Hope this still works!

CollapseExpand
 
matthewbill profile image
Matthew Bill
Technology Leader | Agile Coach | Polyglot Software Engineer | Solution Architect | DevOps Enthusiast | Speaker & Writer | matthewbill.com
  • Location
    London, UK
  • Joined

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"

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Started web dev before table layout was a thing, nowadays a full-stack web/mobile/backend/infra consultant.
  • Location
    Stockholm, Sweden
  • Education
    Computer Science
  • Work
    Full Stack Developer
  • Joined

More fromAndreas Bergström

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp