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"}]}
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)

- LocationStockholm, Sweden
- EducationComputer Science
- WorkFull Stack Developer
- Joined
Hope this still works!

- LocationLondon, 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"
For further actions, you may consider blocking this person and/orreporting abuse