15

I'm trying to run the debugger in VS Code on my nodejs application. I'm using an .env file to store environment variables that I later call with process.env.. When I looked up the VS Code docs for the launch.json, it mentions the envFile option to load the the .envFile. Unfortunately, this is not loading the files when I run the debugger.

launch.json:

{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "node",
            "request": "launch",
            "name": "RegressionTestSuite",
            "autoAttachChildProcesses": true,
            "program": "node ${workspaceFolder}/node_modules/.bin/cucumber-js",
            "args": [
            ],
            "envFile": "${workspaceFolder}/.env"
        },
    ]
}

.env:

export SCREEN_SIZE_WIDTH='1366';
export SCREEN_SIZE_HEIGHT='768';

When I run the VS Code debugger, there are no environment variables from my .env file. How should I be calling the .env file in the launch.json?

Dharman
  • 30,962
  • 25
  • 85
  • 135
Huckleberry Carignan
  • 2,002
  • 4
  • 17
  • 33
  • 1
    At first glance, it looks like your `.env` file isn't setup properly (as mentioned in the answer below). Here are some alternate options using dotenv with runtimeArgs and envFile - https://stackoverflow.com/a/58127078/5623301 - my debugger wouldn't work with the below code but these did. – jgraup Sep 29 '19 at 03:46

2 Answers2

7

I would use the dotenv package to load your .env file, as it can be used by people who aren't using VS Code as well. If you want to include it in your VS Code config, you could do:

{
  "version": "0.2.0",
  "configurations": [
    {
        "type": "node",
        "request": "launch",
        "name": "RegressionTestSuite",
        "autoAttachChildProcesses": true,
        "program": "node -r dotenv/config ${workspaceFolder}/node_modules/.bin/cucumber-js",
        "args": []
     },
   ]
}

Your problem could also be that your .env file should not contain export and semi-colons, as it is not a JavaScript/shell file:

SCREEN_SIZE_WIDTH=1366
SCREEN_SIZE_HEIGHT=768
austin_ce
  • 1,063
  • 15
  • 28
7

You can try this to load the env file.

 {
  // Use IntelliSense to learn about possible attributes.
  // Hover to view descriptions of existing attributes.
  // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
  "version": "0.2.0",
  "configurations": [
    {
      "type": "pwa-node",
      "request": "launch",
      "name": "Launch Program",
      "skipFiles": ["<node_internals>/**"],
      "program": "${workspaceFolder}\\Chat\\server.js",
      "envFile": "${workspaceFolder}\\Chat\\.env"
    }
  ]
}
Lokesh
  • 209
  • 4
  • 2