Huckleberry Carignan
Huckleberry Carignan

Reputation: 2318

In VS Code-debugger, how do I use envFile in launch.json for nodejs?

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?

Upvotes: 24

Views: 38492

Answers (2)

austin_ce
austin_ce

Reputation: 1152

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

Upvotes: 10

Lokesh
Lokesh

Reputation: 317

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

Upvotes: 17

Related Questions