Answer a question

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?

Answers

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
Logo

开发云社区提供前沿行业资讯和优质的学习知识,同时提供优质稳定、价格优惠的云主机、数据库、网络、云储存等云服务产品

更多推荐